5
votes

I've one SignalR hub that manages one type of messaging. I need to add a second hub to the same page which is just a post a message and returns to the rest of the clients and a posted message.

Is it possible to have multiple hubs for one connection? The deal is that if one hub is started before the others how do I connect or subscribe to the current hub if the first hub has already started the connection?

They are both a messaging type of hub but one many or may be used.

How do I approach this?

AlumCloud

1
love the userNameJacobIRR

1 Answers

14
votes

To have multiple hubs for one page you do not need to do anything special, just create another hub class.

To ensure that you can receive messages from a hub in general ensure that you have registered a client side method for the hub prior to starting your connection.

Therefore, if you want to use multiple hubs, all you need to do is have client methods for the hubs you want to subscribe to prior to starting your connection.

No matter how many hubs you are subscribed to they all use one connection which is pretty nice :).

To address your issue of wanting to "subscribe" to a hub after the connection is started, you can't. An alternative would be to always subscribe to both hubs and use groups to control where data is sent. You can learn more about groups here: http://www.asp.net/signalr/overview/signalr-20/hubs-api/working-with-groups