There are methods to do this in the asp.net membership providers, specifically, IsUserOnline() and something like CountUsersOnline(). The only problem with these methods is that they are really lame. They depend on the membership provider's LastActivityDate() and a window you can set in web.config. In other words, the user is considered online if his last encounter with the membership provider plus the time window in web.config has not expired.
We took this scenairo and made it work for us by setting up a Comet server, and pinging the Web server every ten minutes. When the Web server is pinged, it updates the LastActivityDate of the membership provider.
We set the activity window to 12 minutes, as well as the Session timer. This allows us to determine who is online to an accuracy of aproximately ten minutes.
Here is the line in Web.config:
<membership userIsOnlineTimeWindow="12">
Here is jQuery Comet server:
function getData() {
$.getJSON("/Account/Timer", gotData);
}
// Whenever a query stops, start a new one.
$(document).ajaxStop(getData, 600000);
// Start the first query.
getData();
Here's our server code:
public JsonResult Timer()
{
MembershipUser user = Membership.GetUser(User.Name);
user.LastActivityDate = DateTime.Now;
Membership.UpdateUser(user);
// You can return anything to reset the timer.
return Json(new { Timer = "reset" }, JsonRequestBehavior.AllowGet);
}