Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR connect to multiple servers

Is possible to connect using Javascript client to more than one SignalR servers?

For example:

<script type="text/javascript">
    $.connection.hub.url = 'http://server1.net/signalr';
    var server1Hub = $.connection.server1Hub;

    $.connection.hub.start().done(function () {

    });

    // i need to connect to server2
    $.connection.hub.url = 'http://server2.net/signalr';
    var server2Hub = $.connection.server2Hub;

    $.connection.hub.start().done(function () {

    });
</script>

Trying to connect (again) the second time gives me an error:

'server1Hub' Hub could not be resolved.

Can i create two instances of $.connection ? Obviously i think modifying the same connection can create many issues.

like image 556
Catalin Avatar asked Mar 17 '23 14:03

Catalin


1 Answers

Using different $connection:

var connection1 = $.connection('/first');
connection1.start();

var connection2 = $.connection('/second');
connection2.start();

Subscribing on multiple hubs:

var connection1 = $.hubConnection("'http://server1.net/signalr");
var connection2 = $.hubConnection("http://server2.net/signalr");

var Hub1= connection1.createHubProxy('Hub1');
var Hub2= connection2.createHubProxy('Hub2');

connection1.start();
connection2.start();

Read more here: here at section Define method on client (without the generated proxy, or when adding after calling the start method)

like image 163
SilentTremor Avatar answered Mar 19 '23 03:03

SilentTremor