views:

67

answers:

3

Hello everyone,

I am using ASP.Net + .Net 3.5 + VSTS 2008 + IIS 7.0 + C# to develop a web application. I want to count how many users are online. This is my current implementation,

  1. when Session_Start is called, I will increase # of users online by 1;
  2. when Session_End is called, I will descrease # of users online by 1.

Two quesitons,

A Is that implementaiton correct?

B. Another question is, I think this method can not track # of users of real time, since when user closes the browser, Session_End will not be called immediately (Session_End will be deferred to be called). Correct?

thanks in advance, George

+2  A: 

Are you using ASP.NET membership?

Membership.GetNumberOfUsersOnline()

You can customise the amount of time a user is considered to be "online" in web.config - eg

<membership defaultProvider="..." 
            userIsOnlineTimeWindow="10" 
  <providers>
    ....
  </providers>
</membership>

Read more here

Jon
If he use Membership provider, this would be the easiest one to use
D.J
If I use membership provider, does membership provider have an API to get current # of users online? –
George2
Why Membership provider is more reliable? When end user browser is closed, the online # of user could be decreased immediately?
George2
@George2 it really depends why you want the feature. Unless you need it to be as super-accurate as possible for some specific reason, I personally would go for this approach - it's simply code that works that you don't have to write...
Jon
Thanks, I do not want to have super accurate feature. I know membership, but how to implement online user number counter by using membership provider(e.g. sample code or which API could be used to get # of users online), any ideas?
George2
Membership.GetNumberOfUsersOnline can only count # of online logged in user, correct? No solution for non-logged in user (I am not sure if membership provider will insert browser cookie to enable this feature)?
George2
@George2 ahh, yeah, looks like you're right, logged in only - didn't realise you wanted to count anonymous users too...
Jon
Hi Jon, logged in user conuting is fine. I want to know if the same user is logged in a couple of times (e.g. on several browser tabs on the same machine, or even logged in from several macines), will online number of user be counted only once, or counted many times for the same user because of logged in at the same time for several sessions?
George2
Assuming you are using the default `AspNetSqlMembershipProvider` - once per user regardless of browsers/machines, it works based on a per user field in the database. If you're using a custom provider, it depends how that provider implements it...
Jon
Thanks Jon, the last question, for custom membership provider, how to implement so that in my multi-browser tab and multi machine scenario, the user is counted only once?
George2
@George2 I couldn't possibly answer that without knowing how your custom provider is implemented...
Jon
+1  A: 

Another problem with using Session_End is that it does not fire reliably, in fact if you use anything but inproc session state you cannot rely on this at all.

A better option is to use the the Membership provider, take a look at Membership.GetNumberOfUsersOnline()

Chris Taylor
Why Membership provider is more reliable? When end user browser is closed, the online # of user could be decreased immediately?
George2
If I use membership provider, does membership provider have an API to get current # of users online?
George2
@George2, Membership provider is more reliable because it will work consistently, now it will not know immediately that the user is offline, but once the "UserIsOnlineTimeWindow" has expired it will the user will no longer be counted as online. Not instantaneous, but consistent.
Chris Taylor
Hi Chris, could you show me a sample how to implement online user counting feature by using membership provider (e.g. sample code or which API could be used to get # of users online)? I have used membership provider before, and only used authentication provider feature before.
George2
Membership.GetNumberOfUsersOnline can only count # of online logged in user, correct? No solution for non-logged in user (I am not sure if membership provider will insert browser cookie to enable this feature)?
George2
@George2, if you want to count the current number of unique visitors that are online, then I would suggest you add a cookie to the request and count he cookie only once. You could create a GUID and store that in the database with a last access time, every request from the user with that cookie will update the last access time in the database, if the last access time is greater than some time you define then the user is no longer counted as online. You can clear the records from the database if they are older than some defined period.
Chris Taylor
Hi Chris, for your solution, the biggest issue I think is for anonymous user, the cookie could only be used for a limited time, and after the limited time, cookie will expire. Then the same user will get a new cookie, so the the same online user is counted twice? Is that possible? Thanks!
George2
@George2, on each request you check the cookie and you renew it for the anon user.
Chris Taylor
+1  A: 

Your implementation won't be very accurate - if a user logs on using two browsers your counter will increase by two.

A lot of sites just record logons and then do a count of the number of unique logons in the last x minutes to get an online total.

Damien Dennehy
What is your proposed solution?
George2
It depends on how accurate you want the system. For almost every system I've worked on, it was acceptable to simply log all successful user logons to a datasource (database, XML file, etc). You can then query this datasource and bring back a list of online users. Personally I don't like tracking logoffs as there can be too many variables involved.
Damien Dennehy
In your solution, how could we know if a user is not online? e.g. supposing the user closing the browser directly, and does not press logout button, from which we can log user offline event)?
George2
There is absolutely no 100% reliable way of tracking when or how a user logs out. They could have closed their browser, it might have crashed, their network connection may have died, etc.
Damien Dennehy
I think your solution is based on logging user activities. Could you show me more details what activities do you log, so that you can use the log to get # of users online?
George2