Detecting leaving a page

5 views
Skip to first unread message

Tristan Sloughter

unread,
Nov 10, 2009, 2:57:30 PM11/10/09
to nitro...@googlegroups.com
I'd like to be able to detect when a page is let in Nitrogen. Is this possible? The reason is I start a process on the backend for some pages to allow quick access to data for that particular page. But I'd like to be able to terminate the gen_server when that page is left. So I store the Pid in wf:session and would then like to send it a message to stop if the user goes to another page or closes that tab/window.

Thanks,
Tristan

Paul R. Bernard

unread,
Nov 10, 2009, 4:43:24 PM11/10/09
to nitro...@googlegroups.com
Tristan Sloughter <kungf...@gmail.com> writes:

> I'd like to be able to detect when a page is let in Nitrogen. Is this
> possible?

It's a bit kludgy but if you take Tom's comet fork of nitrogen and set
up comet on the page (It doesn't actually have to do anything.) you can
reliably (as far as my testing goes) detect when the page is
abandoned/replaced/network partition/...

If you check back in the archives for one of my comet posts there's a
clause in my listener that goes like:

listener( Agent, Ref, #state {header = ControlID,
locus_id = Locus_id}=State ) ->
receive

{'DOWN', Ref, process, Agent, _Info} ->
%% user has left the page.
exit( done );

...


That message is a result of a 'monitor' on the comet agent. It dies
within 30 seconds or so (I've forgotten) of the page being left.
Sometimes I use this for cleanup like you want.

I've not stumbled across any other method

- paul

Tristan Sloughter

unread,
Nov 10, 2009, 6:13:50 PM11/10/09
to nitro...@googlegroups.com
Cool, I'll give this a try and let people know how it goes.

Tristan Sloughter

unread,
Nov 12, 2009, 11:49:15 AM11/12/09
to nitro...@googlegroups.com
So, I actually changed how the site works. Now the server is pure session, makes much more sense :). So the gen_server only needs to go down when the session times out. Is there a way to specify a function to run on session timeout to do some cleanup?

Tristan
Reply all
Reply to author
Forward
0 new messages