Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

.NET

Managing User Sessions in AJAX Applications


Since the dawn of Web, each request for a page required just one page visit. You set the address, hit the Go button, wait for the page markup to download, see the output. You're happy. You read the page, you click a button somewhere and you either revisit (re-download) the page with some updated contents or you move to another page. It's the same user's session, as long as not much time has elapsed between two successive visits.

By default, a user's session times out after something like twenty minutes of inactivity. More precisely, if the user doesn't post back to the server for more than the specified number of seconds, within a given session, the session is considered expired. Any successive call from the same client is refused with a session timeout error. This is the Web, and it's always been like this. Until AJAX came along.

With AJAX, each page visit—defined as the action of pointing the browser to a specified page—may originate multiple requests and, of course, all of them are from within the same user's session. The beauty of AJAX is that users can interact with the page functions without fully refreshing (i.e., revisiting) the page itself. However, the user can also operate within the page locally without the need of posting back for many minutes.

Honestly, with the old Web model, it's hard to imagine a user that sits still in front of her browser looking at the same page for more than just a few minutes. Session timeouts typically originate when the user leaves the workstation, gets engaged in a long phone call or a nice chat with a colleague, or really spends a lot of time editing some text in the page. In general, behaviors that may originate a session timeout are a kind of a special event in a classic Web applications—occasional but not rare. In AJAX applications, many more events, and frequent indeed, may originate a session timeout. In an AJAX application, the likelihood that a user is actively working with the page but doesn't post back is definitely higher than in a classic Web application.

In this context, a new problem arises—how can you keep the session alive when the user may not post back for a while? Actually, there are not many tricks you can play here; yours is a kind of set route. As a developer, you have to ensure that the session is maintained alive on the server if this is essential for the page's functionality. There's a simple way of doing this—set up a client timer and warn the user a few minutes before the session expires. In ASP.NET, a session expires after 20 minutes, even though this is a configurable parameter that a developer knows. In the end, if the user hangs on a page for about the time it takes to expire the session, you present a message and ask what to do. What's required to keep a session alive? A postback to the Web server is enough. Seems to be the perfect solution? Read Eric Pascarello's Session Warning to Developers for potential drawbacks with this trick.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.