How to find how many people are online on a website

I need to show how many people are online on this site. The site was developed using ASP.NET 2.0. I am currently using the start of session (increase by 1) and the event of end of session (decrease by 1) in Global.asax. But Session_End Event is not called correctly in most cases. Is there a better way to achieve this?

+4
source share
4 answers

You will not be much better than increasing in Session_Start and decreasing in Session_End if you do not use other means, such as a database.

When you check your queries, you can update the timestamp in the database to show that the user is active, and after a specified time (for example, 15 minutes), a query that collects the number of concurrent users ignores this row in the database (thus decreasing the counter).

+5
source

A quick Google search showed a convenient way to do this using the HttpModule.

All in all, Johann did a great job of this. It implements the specified timeout, which was proposed above, but otherwise there is no established way to do this exactly outside of checking perfmon.exe server and checking the number of current connections WebService โ†’ WebAppPool.

When I implemented this myself, I used a SQL Server table to store date / time and user information for authentication. I reduced the score by re-evaluating and matching IP addresses whenever I had to update the data cache (once every 10 minutes).

+4
source

We have one and the same problem in the project, after we tried several methods, we finish tracking the downtime of each user, when the downtime exceeds the session timeout, we consider that the user is no longer online. Of course, you also need to consider another problem, for example, logging out or returning to the system after a timeout ...

+3
source

I have done this before and can confirm that Session_End will only be called if you manually destroy the session (Session.Abandon). When you think about it, it makes sense. If the user is not on the website, the code will never be executed.

What I did was to store the hash table in the application state, containing the username and date-time the last time the user was seen on the site. For each page load, I would call a function that either inserted or updated this value for the current user. Then it will select the entire list and delete all records that are older than the session timeout (20 minutes or something else). Remember to use lock or sync to avoid race conditions when making changes to this list.

This has the added benefit of not only knowing how many people, but specifically which users.

If you do not have something unique, such as Username, you can use Session.SessionID. It must be unique to your site visitor.

But be careful, using the state variable of an instance of an application or application has its share of problems, since it will not be shared between processes in "Web Garden mode" or in configuration with multiple servers. For larger settings, you will need a more persistent medium, such as a database or distributed cache.

+3
source

All Articles