7

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

harriyott
  • 10,505
  • 10
  • 64
  • 103
pajo
  • 173
  • 2
  • 7
  • You could use a Dictionary> 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 02 '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 03 '12 at 08: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 06: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 06:27

2 Answers2

9

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

davidfowl
  • 37,120
  • 7
  • 93
  • 103
4

Found how to workaround:

Rewrite ConnectionId, so in every tab you'd have the same ConnectionId:

 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());

I managed to open as much tabs, as I could and all tabs get notifications.

Eddie
  • 35
  • 1
  • 7
Dmitry
  • 661
  • 5
  • 21
  • you could give full code to understand like how to implement ConnectionFactory who are new to signalr. thanks – Mou Jan 30 '15 at 09:08