Reputation: 15457
How would I keep track of number of current sessions on my website?
Upvotes: 5
Views: 1102
Reputation: 33098
One simple way would be to write a small http module (or even take advantage of the global.asax) that manages when session is created and when session ends to update a collection or database table of all the currently active users and when the session ends to remove the record.
RE: When the session ends you should still have access the session itself or at the very least the session identifier from either the sender property or the eventargs property. This will give you an easy way to associate an asp.net session to a user by using the session identifier. You can easily test this by starting your application in debug and in the immediate window do Session.Abandon(); to trigger this event programmatically to see it happen with live code that you can debug.
Note I'm not 100% sure about using the sender/args but you should be able to access the session itself directly inside the global.asax SessionEnd from just doing this.Session which should still be available (make sure to not use HttpContext.Current.Session because the context does not exist then).
Upvotes: 0
Reputation: 13230
One thing you could do is implement your own SessionStateStoreProvider: http://msdn.microsoft.com/en-us/library/ms178587.aspx
In your implementation of GetItem, you would update your own collection of current session data and in your RemoveItem implementation, you have access to the ASP.NET Session data, so you could remove the item from your collection before the removal of the ASP.NET Session in the usual way. You could query your collection for the currently active sessions.
Alternatively:
If you are using the ASP.NET Membership provider, you can query your database with something along the lines of:
Create Procedure dbo.GetCurrentUsers
@ActiveSince DateTime
AS
SELECT U.UserId, U.UserName, M.Email
FROM aspnet_Users U
INNER JOIN aspnet_membership M ON M.UserId = U.UserId
WHERE U.LastActivityDate > @ActiveSince
GO
You would set @ActiveSince to be the current DateTime minus the Session timeout on your site.
var sessionState = (System.Web.Configuration.SessionStateSection)
ConfigurationManager.GetSection("system.web/sessionState");
DateTime activeSince = DateTime.Now.AddMinutes(0 - sessionState.Timeout.TotalMinutes);
This will include users who have gone to your site and then left straight away, but there is no way of telling whether they are still there, unless you have a script on your page that regularly sends an aJAX call to the server saying that it is still open. I would recommend against this as it would increase traffic, and it wastes resources for no real gain.
Upvotes: 0
Reputation: 2842
I would handle this mostly within Global.asax. Have Session_Start add to an Application global list containing the data you want to track (ie User, SessionId, StartTime, LastRequestTime).
When a User first hits the site, Session_Start will add an entry to the GlobalList. This will be sure that every unique user gets it's own entry (handle annoymous as well). If you'd like, once a user logs in, you can update the GlobalList to set the User info (Name, ID, whatever).
Next, have Application_Start start a background thread that goes through the global list and removes any items that are expired (based on whatever threshold you set, TimeSpan of LastRequestTime-StartTime).
If you want to deploy this in a WebFarm scenario, then you'd have to move the list to your database.
Upvotes: 0
Reputation: 86504
If you just want to know who's looking through pages on a little rinky-dink site, one way is to have an app-wide (or static) list of the latest requests, along with the date/time of the request. Whenever someone requests a page, remove all of the "old" hits (older than X minutes), and append (or update) the visitor's info with a date/time of now,
Note, for a really busy site, this would probably be a bad idea. A somewhat more scalable solution would be to have a 'last visit' column in your users table, and update that whenever the user requests a page. But that wouldn't be helpful to track anonymous/not-logged-in users.
Either way, to see who's "active", you go through the data and find all the visits/users with a last-visit time less than X minutes ago, where X is some number you feel is appropriate. 20-60 minutes is usually good enough.
Upvotes: 2
Reputation: 9005
A very simple method is to keep track of the last activity by each user. After a period of no activity, you say that that user is no longer active.
Upvotes: 0