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
In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).
SignalR supports full duplex communication that allows the client and server to transfer data back and forth.
ASP.NET SignalR is a new library for ASP.NET developers that makes developing real-time web functionality easy. SignalR allows bi-directional communication between server and client. Servers can now push content to connected clients instantly as it becomes available.
SignalR uses Web Sockets and the HTML5 API that helps in bidirectional communication. It also provides an API for server-to-client Remote Procedure Calls (RPC) call, it may be something new for you because most of the time we use a request and response model.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With