Get the number of users that are online using SignalR

In a recent experiment, I was looking for a way to retrieve the number of online users in an ASP.NET application. There are a number of techniques that can be applied, such as incrementing based on ASP.NET session state, or using Google Analytics to keep track of active users. I wanted to build an example that did not require ASP.NET session state, was accurate, and updated in real-time.


This isn't as easy as it sounds! I tried using various different approaches, but accuracy proved problematic as each approach had it's own disadvantages. Fortunately, SignalR came to the rescue!

Using SignalR to get real-time user count

If you aren't already familiar with SignalR, it is an awesome library that allows ASP.NET developers to add real-time web functionality to their applications. For more detail about the library, check out the SignalR website. The beauty of SignalR is that it uses a fallback mechanism to connect the browser to the server. A SignalR connection starts as HTTP, and is then promoted to a WebSocket connection if it is available. This means that if the browser doesn't support WebSockets, it will fallback to the next available transport mechanism. This guarantees that you will get results, regardless of the browser.

SignalR has some pretty nifty methods that are able to keep track of every time that a user connects, disconnects or reconnects to a web page. By overriding these methods, you can hook in and keep a count of the number of users online.

Get started by adding the SignalR library to your application. Use the Nuget package for convenience.

SignalR Nuget Package

Once you have added the SignalR library to your application, you will need to create the class that contains the SignalR hub. When this method is called, it will update all listeners with the new updated count of users that are online. This method can be called from anywhere in the application and every time that an associated event occurs.

In the code above, each time that a user connects/disconnects to the web page, the SignalR hub will identify them by the unique Client Id. This Client Id is then added to a list of users, which we can easily retrieve the count of.

In order for the SignalR library to work in your application, you will need to update the Global.asax file in the project to map to the SignalR hub.

Next, update the page that you would like to display the user count. You will need to reference the SignalR clientside JavaScript and start the connection to the hub.

Once all of the code is in place, you should have a real-time, accurate count of the users that are using your application. An easy way to test this is to fire up a number of browsers and watch the user count increase and decrease as you close and open windows.

SignalR Cross Browser

If you would like to take a look at the source code used in this example, please check out the repo on Github. If you haven't used SignalR before, go and check it out! It's a great library that provides a really easy way to add real-time functionality to your site.

Comments - 9/19/2013
Thanks for the link to Github. I've never used SignalR before, so I'm glad to have an opportunity to spice up my site. - 9/19/2013
Thanks for the link to Github. I've never used SignalR before, so I'm glad to have an opportunity to spice up my site.

Chris - 9/21/2013
What exactly is 'clientId'? I don't see it getting set in the sample code. Is it for tracking logged on users? I've just started using SignalR -- this sample helps. Thanks

Dean Hume - 9/22/2013
@Chris - The ClientId is a unique ID that is used to identify each user that connects to the page. You can see it in the Context.ConnectionId.

Chris - 9/22/2013
@Dean, I was wondering why you had the 'clientId' test against QueryString. if (Context.QueryString["clientId"] != null) Thanks

DEBAL - 11/18/2013
Hello Dean, I am new in developing I want to know can we use SignalR for notification like facebook notification ? Or can we use this for on line chat communication

Arif - 1/16/2014
Hi, I am using your code - but it seems there are some issues : - For chrome when I refresh the page the counter increases - If I open the page on separate tab on any browser then counter increases How can I resolve this ?

Johnny - 7/19/2014
Do I have to use jquery? My app is currently using Ajax control Toolkit.

Michael - 11/10/2014
Interesting idea but I don't think this will work in practice. Here are the issues you will run into: - SignalR reissues authentication tokens so this will result in users never logging - ClientId in a local variable won't scale to multiple servers

Mike - 4/1/2015
hi, few things that I've noticed in your code that you should defo re-think -> you do not need to retrieve the context via GlobalHost.ConnectionManager, you are in the hub clas!!! simply use this, so this.All... etc. since you've made your list static, better lock it prior to use, not safe atm...

Ivan - 3/3/2016
Mike's right. 1. No need to grab the context. 2. Possible multithreading issue

Wiep Corbier - 6/14/2016
I use similar code to show the user online, but currently I have a negative count of -23. How is that possible?

Bob - 8/9/2016
Can i use SignalR to notify/alert logged in users when my site is under going maintainance.

Dipak - 8/16/2016
This was really helpfully but instead of showing the total number of users i want them as list just like facebook will give so can you guide me how to perform this

Mike - 9/2/2016
Any advice on how to do this on a backplane setup?

Nguyen Van Nam - 9/3/2016
Thanks you code for you <3

Shami Sheikh - 11/25/2017
@Dean, This was really helpfull to show number of online user for my website . But now i want to display all the users who are online by therir name . Can you give me an idea how to manage this to display online status for particular user. I am using a table to maintain this but i am unable to use session that is why i could not do so. Please help me i will be highly obliged to you.

PastorBrad - 1/12/2018
All 4 of these children were taken from their mother who doesn't have custody and fled out of the county with them. July 4th they left from Lost Angeles airport and their final destination was Paris France. We do not know the whereabouts they may be now. They could be anywhere in the world. Our Facebook page:

Add your comment

300 Characters left

Please fill this in to confirm that you are human