Take the 2-minute tour ×

Here is my problem, I want to track if user is online or offline and notify other clients about it. I'm using hubs and implemented both IConnected and IDisconnect interfaces.

My idea was to send notification to all clients when hub detects connect or disconnect. By default when user refreshes page he will get new connection id and eventually previous connection will call disconnect notifying other clients user is offline even though he's actually online.

I tried to use my own ConnectionIdFactory returning username for connection id but with multiple tabs opened at some point it will detect user connectionid disconnected and after that client side hub will try to unsuccessfully connect to the hub in endless loop wasting memory and cpu making browser almost unusable. I needed to fix it fast so I removed my factory and now I add every new connection to the group using username, so I can easily notify single user on all connections, but then I have problem of detecting if user is online or offline as I don't know how many active connection user is having.

So I'm wondering is there a way to get number of connections in one group? Or if anybody has some better idea how to track when user goes offline?

I'm using Signalr 0.4

share|improve this question
You could use a Dictionary<string, HashSet<string>> where the key is the username and the hashset contains the connectionId(s) for that user. # of users = Keys.Count(), # of connections = dict.Values.Sum(x=>x.Count()); –  drch Apr 2 '12 at 19:47
Site should work in web farm scenario, so I can't use Dictionary. I could store connections in db, but I would like to avoid this if possible. –  pajo Apr 3 '12 at 8:24
When you say user refresh the page and he gets new connection id, I am storing connection ID in my DB and if user view two pages on my site where my hub is same the connection ID is same as well. Why are you getting separate connection ID. I think you can only get different ID when client is different. –  Farrukh Subhani Nov 29 '12 at 6:25
To say how to track online offline i just store username and connectionID in DB. In another app i create a group as a role so user can talk within their role or to super admin or IT just by knowing their role. I am using ASP.net membership and Active Directory. Works in both. –  Farrukh Subhani Nov 29 '12 at 6:27
add comment

2 Answers

up vote 6 down vote accepted

There's not way to do this other than counting on your own.

share|improve this answer
add comment

Found how to workaround:

Rewrite connectionId, so in every tab you'd have the same sonnection id:

 public class MyConnectionFactory : IConnectionIdGenerator
        public string GenerateConnectionId(IRequest request)
            return MyUserManager.Instance.CurrentUserID.ToString();

Add to global.asax:

GlobalHost.DependencyResolver.Register(typeof(IConnectionIdGenerator), () => new MyConnectionFactory());

And I managed to open as mach tabs, as I could. And all tabs get notifications.

share|improve this answer
Does not work with latest Signalr Version. –  kravits88 Apr 24 at 8:14
add comment

Your Answer


