angularangular5signalrsignalr-hubsignalr-2

Angular with signalR: Multiple hubconnections to the same route


I have my client code as below. I have a signalr route and hub connection name. I am able to connect to the hub and get events. Now I need to connect to one more hub for the same route to get more events.

  this.hubConnection = this.window.$.hubConnection();
    this.hubConnection.url = this.apiUrl  + "api/signalr";
    this.hubProxy = this.hubConnection.createHubProxy("Event1Hub");
    this.hubProxy = this.hubConnection.createHubProxy("Event2Hub"); // When I add this line it is overriding first hub and only connecting to Event2Hub.

I am using "signalr": "2.3.0",


Solution

  • You are creating a proxy for Event1Hub and then reassining that object instance with proxy for Event2Hub. If you want to communicate with both hubs you'll have to initialize separate proxy objects in order for this to work.

    this.hubConnection = this.window.$.hubConnection();
    this.hubConnection.url = this.apiUrl  + "api/signalr";
    this.hubEvent1Proxy = this.hubConnection.createHubProxy("Event1Hub");
    this.hubEvent2Proxy = this.hubConnection.createHubProxy("Event2Hub");
    

    This way hubEvent1Proxy will provide Event1Hub communication while hubEvent2Proxy will provide Event2Hub communication.