4

Now that the new version of SignalR has done away with the IConnectionFactory interface, what is the recommended pattern to use to set the connection id of a client?

Heather
  • 2,602
  • 1
  • 24
  • 33

1 Answers1

8

There is no longer any recommended pattern for setting the connection id of a SignalR client in 1.0.

The best practice is to maintain state on your SignalR server that maps your application's users to a connection id. The obvious place to add connection ids to this map is in OnConnected. You can use OnDisconnected to remove connection ids.

Another option, which could also be done in OnConnected, is to add the client's connection id to a group with the name you would have created for the client in IConnectionIdFactory.CreateConnectionId before. Naturally, that group would only contain the one client, so you can send all the messages you would have sent to the custom connection id to the group instead.

Note: If you go with the mapping option you might also attempt adding connection ids to the map in OnReconnected if you are concerned about AppDomain restarts and you are storing this state in some sort of static variable instead of something more persistent. Obviously you would need shared state for this if you are scaling out SignalR.

halter73
  • 15,059
  • 3
  • 49
  • 60
  • Thanks for answering. I just wish I liked the answer better =) – Heather Jan 17 '13 at 00:01
  • Yes, I am also disturbed that you removed this IConnectionIdFactory interface. Why don't you let users use this one or the new one as they want ? Thx – Hassan Jan 21 '13 at 09:46
  • Does this also mean that SignalR doesn't work (tried this on Chrome and FF only, don't know other browsers) with more than 5 tabs open?? If we have a different connectionid for each tab and Chrome can open a max of 6 connection to the same server... ouch! Any workaround for this (with older version of SignalR the workaround was to set connectionid client side). – Pietro Mar 03 '13 at 12:33
  • Creating a group for each client, with different connection id is also a better idea, if optimization is not an issue. – Hemal Apr 01 '21 at 08:05