Heart Beat Design Pattern - Keeping Webpage Session Alive

No.of Views1536
Bookmarked0 times
Downloads 
Votes0
By  amalhashim   On  15 Feb 2010 23:02:10
Tag : ASP.NET , How to
Heart Beat Design Pattern - Keeping Webpage Session Alive
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

Sometimes you want your web page to 'stay alive'. That is, if a user is filling out a complicated form, you do not want the session to time out before they are finished.

It's not simply a matter of increasing the session timeout to a very large value. If you do that, the sessions would be left active in the server memory for hours—long after the visitors have left the site. Increasing the session timeout IS a solution… but not necessarily a good solution.

The goal is that the session should stay active as long as the web page is open on the client machine …even if there are no post backs to reset the session timer. When the web page is closed, the session should time out normally.

I implemented a solution for this: The client will "ping" the server at intervals of less than the session timeout which will reset the session timer. This is known as the Heartbeat design pattern.

For testing purposes, I set the Session Timeout to two minutes in web.config:

{codecitation class="brush: xml; gutter: true;" width="600px"}


<system.web>
  <sessionState timeout="2">
/// 
/// Output Debug String with time stamp.
/// 
 
{/codecitation}

{codecitation class="brush: csharp; gutter: true;" width="600px"}
public static void ODS(string Msg)
{
String Out = String.Format("{0}  {1}", 
DateTime.Now.ToString("hh:mm:ss.ff"), Msg);
    System.Diagnostics.Debug.WriteLine(Out);
}

{/codecitation}

To watch the Session State events, I added debugging strings to the global.asax file:


{codecitation class="brush: csharp; gutter: true;" width="600px"}

<%@ Application Language="C#" %>
 
<script RunAt="server">
 
void Application_Start(object sender, EventArgs e)
    {
MiscUtilities.ODS("****ApplicationStart");
    }
void Session_Start(object sender, EventArgs e)
    {
MiscUtilities.ODS("Session_Start");
    }
void Session_End(object sender, EventArgs e)
    {
MiscUtilities.ODS("Session_End");
    }

{/codecitation}

Here are the details:

We need a method at the server for the client to call. We use a WebMethod.

1. There must be a ScriptManager on the page.
2. The ScriptManager must have EnablePageMethods set to true.
3. The WebMethod must be public and static.
4. The WebMethod must have the EnableSession attribute set to true.

{codecitation class="brush: csharp; gutter: true;" width="600px"}

<asp:ScriptManager ID="ScriptManager1" runat="server" 
EnablePageMethods="true">
<!--asp:ScriptManager>
 

public partial class _Default : System.Web.UI.Page
{
    [WebMethod(EnableSession=true ) ]
public static void PokePage()
    {
// called by client to refresh session
MiscUtilities.ODS("Server: I am poked");
    }
{/codecitation}

We need JavaScript at the client to call the server function at fixed intervals:

{codecitation class="brush: html; gutter: true;" width="600px"}

<script type="text/javascript">
 
var HeartBeatTimer;
 
function StartHeartBeat()
    {
// pulse every 10 seconds
if (HeartBeatTimer == null)
            HeartBeatTimer = setInterval("HeartBeat()", 1000 * 10);
    }
 
function HeartBeat()
    {
// note: ScriptManger must have: EnablePageMethods="true"
        Sys.Debug.trace("Client: Poke Server");
        PageMethods.PokePage();
    }

 
<body id="MyBody"onload="StartHeartBeat();">

{/codecitation}

Here is what the output looks like without the heartbeat:

10:22:43.03 ****ApplicationStart
10:22:45.13 Session_Start
10:25:00.00 Session_End

Here is the output with the heartbeat:

10:26:06.10 ****ApplicationStart
10:26:08.05 Session_Start
Client: Poke Server
10:26:18.93 Server: I am poked
Client: Poke Server
10:26:28.95 Server: I am poked
Client: Poke Server
10:26:38.96 Server: I am poked
Client: Poke Server
10:26:48.98 Server: I am poked

. . . (lines deleted)

Client: Poke Server
10:29:59.45 Server: I am poked
Client: Poke Server
10:30:09.47 Server: I am poked
Client: Poke Server
10:30:19.48 Server: I am poked

. . . (lines deleted)

It looks like the session is staying alive while the client is idle: Excellent!

I hope someone finds this useful.

Thank you

Amal

 
Sign Up to vote for this article
 
About Author
 
amalhashim
Occupation-Software Engineer
Company-Aditi Technologies
Member Type-Senior
Location-Not Provided
Joined date-07 Jun 2009
Home Page-http://lamahashim.blogspot.com
Blog Page-http://lamahashim.blogspot.com
I have done my masters in Computer Applications and graduation in Computer Science. I have great passion in working with Microsoft tool and technologies. I am also a Microsoft Most Valuable Professional. Personally my objective is to design/develop applications which eases user experience and performs better in long run.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top