Jul 18 2007

Adding Presence to your web applications

Have you ever wanted to add 'Who's online now' functionality?  This IM-style capability is a type of presence awareness and is being adopted by more and web communities.  Generally speaking this is pretty easy.  In most authentication systems; including those that come standard with ASP.NET; track some type of LastActivityDateTime.  If not, that ok too, because you can add your own DateTime field to your XML auth provider, SQL Accounts system, Active Directory Schema (this is a little harder), etc... 

Once that is in place all you need to do is update the value for users with named identities whenever they make a web request.  I generally tap into HttpContext.Current.User.Identity.Name with an HttpModule and if the user is authenticated, I grab their username and then update the date store.  Here is an abstract example...

Imports System.Web
''' <summary>
''' This module sets the online status and last 
''' activity date/time for logged on users
''' </summary>
Public NotInheritable Class PresenceUpdaterModule
    Implements IHttpModule
    ''' <summary>
    ''' Module Constructor
    ''' </summary>
    Public Sub Init(ByVal context As HttpApplication) _
        Implements IHttpModule.Init
        AddHandler context.PostAuthorizeRequest, _
        AddressOf Context_PostAuthorizeRequest
    End Sub
    ''' <summary>
    ''' Context handler for the PostAuthenticateRequest event
    ''' </summary>
    Private Sub Context_PostAuthorizeRequest( _
        ByVal source As Object, ByVal e As EventArgs)
        If source Is Nothing Then Exit Sub
        If HttpContext.Current.User Is Nothing OrElse _
            Not HttpContext.Current.User.Identity.IsAuthenticated Then
            'Do nothing
        Else
            UpdatePresenceStatus()
        End If
    End Sub
    ''' <summary>
    ''' This updates the users online status 
    ''' </summary>
    Private Sub UpdatePresenceStatus()
        'Add code to update the users LastActivityDateTime
    End Sub
    Public Sub Dispose() Implements IHttpModule.Dispose
        'Dispose resources as needed
    End Sub
End Class

With all of that done all you need to do is create some type of user control that encapsulates reading that value and applying graphics to represent different statuses.  For example...

  • If LastActivityDateTime <= 3 minutes the user is online
  • If LastActivityDateTime > 3 AND <= 5 minutes the user is away
  • If LastActivityDateTime > 5 minutes the user is offline

This is completely arbitrary and you can do what you want...  If you provide some form of a logoff function, you could also set an online/offline flag to track this state and automatically set the user to offline if there are no page requests within the ticket expiration period. 

With a little extra effort you could make your presence avatars link to messaging solutions and help facilitate user collaboration.

Not to overstate the obvious, but some critics may say that presence awareness in a stateless web environment is not really feasible.  For those critics, I would recommend you look at communities like MySpace.com who have become tremendously successful as a result of capabilities such as this.

Tags: ,

Comments

Comments are closed