views:

415

answers:

6

I have a requirement that my site always display the number of users currently online. For example, "35741 Users Currently Online". This is not based on a log in, simply how many users are currently on my site. I have tried using Session Start/Session End for this, however session end is not reliable. Therefore I get inflated numbers, as my session start adds numbers but session end doesn't remove them because it doesn't fire.

There is no additional information to be gathered from this (reporting, etc), it's simply requested that the number show up. Very simple request that's turning into a huge deal. Any help is appreciated.

EDIT:

I should specify that I have also tried using a database for this. Simple table that contains a session ID and a last activity column. With each page hit, I check to see if the session is in my database. If not, insert. If so, update with activity time. Then I run a procedure that sweeps the database looking for sessions with no activity in the last 20 minutes. This approach seemed to kill my SQL server and/or IIS. Had to restart the site.

+2  A: 

Best way is like you do, but time it out via activity. If a given session doesn't access a page within 5 minutes or so, you may consider them no longer active.

Noon Silk
+1 I was writing the same thing
Byron Whitlock
Just as I was going to hit enter...
Dave
I did write the same thing!
Larsenal
Okay...so forgive me for the stupid question, but how do I keep track of the session without using a database? This app is written in .NET 1.1.
mlb
A: 

For every user action that you can record, you need to consider them "online" for a certain window of time. Depending on the site, you may set that to 5 minutes. The actual web request should take less than a second. You have to make some assumption about how long they might stay on that page and do nothing but be considered online.

This approach requires that you keep track of the time of each users last activity.

Larsenal
+1  A: 

Use Performance Counters:

  • State Server Sessions Active: The number of active user sessions.
Remus Rusanu
+2  A: 

If you're using ASP.Net membership, take a look at GetNumberOfUsersOnline.

Scott Ivey
A: 

Expanding what silky said in his answer - since really http is stateless to determine if the user is currently 'online' you can really only track how long since the user last accessed your site and make a determination on how long between requests your consider to still be active.

Since you stated that this isn't based upon users logging in may it's a simple of how many different IP addresses you received requests from in the past 5 minutes (or however long you consider the 'online' timeout to be).

Jeremy Raymond
A: 

Don't use sessions for this unless you also need sessions for something else; it's overkill otherwise.

Assuming a single-server installation, do something like this:

  1. For each user, issue a cookie that contains a unique ID
  2. Maintain a static table of unique IDs and their last access time
  3. In an HttpModule (or Global.asax), enter new users into the table and update their access times (use appropriate locking to prevent race conditions)
  4. Periodically, either from a background thread or in-line with a user request, remove entries from the table that haven't made a request within the last N minutes. You might also want to support an explicit "log out" feature.
  5. Report the number of people online as the size of the table

If you do use sessions, you can use the Session ID as the unique identifier. However, keep in mind that Session IDs aren't issued until you store something in the Session dictionary, unless you have a Session_Start() event configured.

In a load balanced or web garden scenario, it gets a little more complicated, but you can use the same basic idea, just persisting the info in a database instead of in memory.

RickNZ