Coding Tips & Tricks


Home > SignalR

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.

ASP.NET Logo

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

http://devtools.korzh.com/ - 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.

http://devtools.korzh.com/ - 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 ?

harshil - 5/17/2014
hello

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


Add your comment

300 Characters left


Please fill this in to confirm that you are human