1

Let's say I have 2 hubs on my SignalR app. I'm mapping the connectionsIds -> userIds on a local cache dictionary.

Current logic is like so:

With OnConnected --> add a new "connectionId to userId" entry to the dictionary(if it doesn't exists).

With OnDisconnected --> remove that "connectionId to userId" entry from the dictionary (if it exists)

Is there any scenario that Hub1 will be disconnected while Hub2 will stay connected?

If so, my current logic is flawed as it will remove the entry from the dictionary even though Hub2 is still connected. fixing that will require me to handle 2 dictionaries for each hub.

P.S. I've looked at SignalR docs for handling connections mapping and it always seems they are using one dictionary per hub. so I want to know if it's really necessary.

Ofir
  • 517
  • 4
  • 16
  • 1
    Hi Ofir. Just a suggestion, you can look into using Single-User Groups to keep this mapping. It's easier to implement and you don't have to stress yourself with maintaining a dictionary. Check [this](http://stackoverflow.com/questions/37455608/handling-concurrent-connections-in-siganlr/37458129#37458129) – Florin Secal May 31 '16 at 12:46
  • Very elegant solution! I think I'll go that path. thanks – Ofir Jun 01 '16 at 08:42

1 Answers1

1

According to the documentation Signalr share one connection for multiple hubs. So if you receive a call to OnDisconnect, all your hubs should be disconnected.

SignalR 2.0 was updated to handle multiple hubs over one signlar connection with no lost in performance.

http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-server#multiplehubs

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36
  • You're right, I've looked at the code on HubDispatcher class, the function gets all hubs and execute the disconnect event on all connected hubs. – Ofir Jun 01 '16 at 08:36
  • ASP.NET Core uses one-connection-per-Hub now, which is a problem because on iOS there's a limit of 4 connections, ugh. – Dai Nov 12 '20 at 05:48