8

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 Answers1

16

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

N. Taylor Mullen
  • 18,061
  • 6
  • 49
  • 72
  • Yea, once you get the concept it really is nice. I've just got to get created and figure out how to subscribe to all me client code before the connection is started. Would a iframe work to have separate hubs with separate connections? – Filling The Stack is What I DO Nov 07 '13 at 15:07
  • lol ya you could take that approach, it does seem a little overkill though. – N. Taylor Mullen Nov 07 '13 at 18:12
  • 1
    It is not fully true that you need to have all client methods subscribed before starting your connection. You can do it using "without generated proxy" after connection is started. Refer to SignalR guide at http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server for details. – newman Sep 20 '14 at 19:36
  • 1
    If you want to subscribe to a hub after the connection is started, you can call ```$.connection.hub.stop()``` and then ```$.connection.hub.start()``` again, it will have the new hub. – LongZheng Aug 06 '15 at 03:16