gsharp
gsharp

Reputation: 27927

signalR core open connection twice

Is it allowed to create two connections to the same hub? Or should I use the same Hub connection?

var cnn1 = new signalR.HubConnection("/hub1");
cnn1.on("Method1", function(){});
cnn1.start().then(function() {cnn1.invoke("Something1");});

var cnn2 = new signalR.HubConnection("/hub1");
cnn2.on("Method2", function(){});
cnn2.start().then(function() {cnn2.invoke("Something2");});

Upvotes: 2

Views: 2254

Answers (2)

Stephu
Stephu

Reputation: 3334

SignalR 2.0: Multiple hubs can share one connection

Yes you can do that. But take care about the limitations from server (IIS and Browser)

You can have multiple hubs sharing one connection on your site. SignalR 2.0 was updated to handle multiple hubs over one signlar connection with no lost in performance."

You find this answer in the following question: Multiple signalR connections/hubs on your website

SignalR Core: Does NOT support having more than one hub per connection

The new version of SignalR does not support having more than one Hub per connection. This results in a simplified client API, and makes it easier to apply Authentication policies and other Middleware to Hub connections. In addition subscribing to hub methods before the connection starts is no longer required.

Read more information about signalr core: https://blogs.msdn.microsoft.com/webdev/2017/09/14/announcing-signalr-for-asp-net-core-2-0/

Remark:

Once I started with an application with signalr 2.0 where I also planned to use multiple connections. But I had some strange effects because limitations of browsers. The browsers do limit the number of paralell connections. Maybe this is not a use case for you that you have multiple browser instances on same browser open, but you should know this as background. You can test this if you open 10 instances of the SAME browser type. Then you can see that not all instances will create a connection.

Upvotes: 0

Brennan
Brennan

Reputation: 1933

You're perfectly fine creating 2 HubConnection instances. In this simple example there is no reason to do so, but I'm sure there are cases where you would want to.

Side-note, you're aware the connection can have multiple .on() methods registered? cnn1.on("Method1", function(){}); cnn1.on("Method2", function(){});

Upvotes: 3

Related Questions