469,903 Members | 1,808 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

AJAX: Clientcallback End session on browser close

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
--
Jerry
Feb 28 '07 #1
4 2532
Hi,

Jerry C wrote:
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
Feb 28 '07 #2
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 [MVP]" wrote:
Hi,

Jerry C wrote:
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
Feb 28 '07 #3
Hi,

Jerry C wrote:
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
Feb 28 '07 #4
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 [MVP]" wrote:
Hi,

Jerry C wrote:
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
Feb 28 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by melledge | last post: by
4 posts views Thread by rgparkins | last post: by
2 posts views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
4 posts views Thread by KDawg44 | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.