2

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");});
gsharp
  • 27,557
  • 22
  • 88
  • 134

2 Answers2

3

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(){});

Brennan
  • 1,834
  • 1
  • 7
  • 13
  • Yes the sample is easy just for StackOverflow purposes. However in my app cnn1 is in a global js file, that is loaded for every resource, and cnn2 is in another script that is loaded just for a specific resrouce. That's why I asked if it's ok, or if i should reuse the same connection / create a second hub. there's no much documentation around yet since it's still alpha version. – gsharp Jan 26 '18 at 04:58
  • Like I said, its ok to do either method. Having 2 connections might allocate more resources on the client, but could possibly have more throughput. You'd have to measure that though. – Brennan Jan 26 '18 at 16:31
0

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.

Stephu
  • 3,236
  • 4
  • 21
  • 33