AJAX: Clientcallback End session on browser close

G

Guest

I have seen several articles about this subject but I was wondering with ajax
is this easer. The articles mentioned client callback and the onbeforeunload
event of the browser. Can AJAX be used for this and are there any examples to
review.

Thank you
 
L

Laurent Bugnion [MVP]

Hi,

Jerry said:
I have seen several articles about this subject but I was wondering with ajax
is this easer. The articles mentioned client callback and the onbeforeunload
event of the browser. Can AJAX be used for this and are there any examples to
review.

Thank you

The browser events are not reliable (and also not all are cross-browser
compatible). So using a onunload or onbeforeunload to end the session is
not fool proof.

If you want a sure way, you must use a kind of heart beat. Have the web
client call a web method (might be as simple as a ASHX custom handler
call). As long as the heart beats, the session will not end. If the
heart stops beating, then the session will end automatically after the
Session Timeout set in the web.config. If you want the session to end
immediately, then you'll need to implement some more code, for example a
working thread checking periodically which hearts are still beating.
However, if you set a session timeout of 2 minutes, and the heart beats
every 1 minute, then the waiting time is not that big, and you get the
session to end automatically.

HTH
Laurent
 
G

Guest

Laurent,

Thank you for the reply. I would not have thought of the heartbeat solution.

I have found a article about client callbacks
http://msdn2.microsoft.com/en-us/library/aa479042.aspx

This article may help me program it. Examples are a good thing also if you
have them.

I still would like to find some examples of using the onunload events to
communicate to the server to close the session. Learning how to make browser
events trigger server code will help me in other situations.

Thank you
--
Jerry


Laurent Bugnion said:
Hi,

Jerry said:
I have seen several articles about this subject but I was wondering with ajax
is this easer. The articles mentioned client callback and the onbeforeunload
event of the browser. Can AJAX be used for this and are there any examples to
review.

Thank you

The browser events are not reliable (and also not all are cross-browser
compatible). So using a onunload or onbeforeunload to end the session is
not fool proof.

If you want a sure way, you must use a kind of heart beat. Have the web
client call a web method (might be as simple as a ASHX custom handler
call). As long as the heart beats, the session will not end. If the
heart stops beating, then the session will end automatically after the
Session Timeout set in the web.config. If you want the session to end
immediately, then you'll need to implement some more code, for example a
working thread checking periodically which hearts are still beating.
However, if you set a session timeout of 2 minutes, and the heart beats
every 1 minute, then the waiting time is not that big, and you get the
session to end automatically.

HTH
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 
L

Laurent Bugnion [MVP]

Hi,

Jerry said:
Laurent,

Thank you for the reply. I would not have thought of the heartbeat solution.

I have found a article about client callbacks
http://msdn2.microsoft.com/en-us/library/aa479042.aspx

This article may help me program it. Examples are a good thing also if you
have them.

I still would like to find some examples of using the onunload events to
communicate to the server to close the session. Learning how to make browser
events trigger server code will help me in other situations.

Thank you

The major issue with onunload is that it should be also fired when the
browser is closed (using Exit, the "X" button, Alt-F4 or any other
method). However, in that case, the JavaScript engine may very well be
shut down before the XmlHttpRequest has a chance to be sent to the server.

Other events (onload, onresize...) are more reliable. However, having a
safe server-side application is always a good idea, because JavaScript
may be disabled... ;-)

HTH,
Laurent
 
G

Guest

Laurent

Thank you for the reply. The heartbeat solution seems to be the best one for
my application since it is a inside company application and users will leave
the browser open and attend meetings and go to lunch and expect their session
to be active when they return.

I also would like to mention that the URL I left in my last post is old
information. the new AJAX 1.0 is now out and client side stuff is easer. I
have not finished my research on how to do this but searching on the tags for
AJAX like:

<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePartialRendering=true >
<Scripts>
<asp:ScriptReference />
</Scripts>
</asp:ScriptManager>
The Scripts and asp:scriptreference are the key I will post some urls with
the examples when I find them

Thank you for all the help


--
Jerry


Laurent Bugnion said:
Hi,

Jerry said:
Laurent,

Thank you for the reply. I would not have thought of the heartbeat solution.

I have found a article about client callbacks
http://msdn2.microsoft.com/en-us/library/aa479042.aspx

This article may help me program it. Examples are a good thing also if you
have them.

I still would like to find some examples of using the onunload events to
communicate to the server to close the session. Learning how to make browser
events trigger server code will help me in other situations.

Thank you

The major issue with onunload is that it should be also fired when the
browser is closed (using Exit, the "X" button, Alt-F4 or any other
method). However, in that case, the JavaScript engine may very well be
shut down before the XmlHttpRequest has a chance to be sent to the server.

Other events (onload, onresize...) are more reliable. However, having a
safe server-side application is always a good idea, because JavaScript
may be disabled... ;-)

HTH,
Laurent
--
Laurent Bugnion [MVP ASP.NET]
Software engineering, Blog: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top