Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnConnected method not called SignalR when I use shared connection in multiple hubs

We can create multiple hubs for different things, and to connect to each hub we can create multiple client side hubs with sharing connection so that, one connection being made to all hubs. Now, the problem arises that the hub onconnected method is not raising on each hub server side code.

public class Hub1 : Hub
{
        public override Task OnConnected()
        {
            return base.OnConnected();

        }
}

public class Hub2 : Hub
{
        public override Task OnConnected()
        {
            return base.OnConnected();

        }
}

let say, on the client side i create the hub1 and hub2 with client side methods defined on both the hubs, then only one of the hubs onConnected method gets called on server side. If I create the hubs on the client side with separate connections then the OnConnected method gets called. So, there any other work around if I want to use the same connection for each hub but also would like to raise the on Connected event of separate hubs.

like image 580
Manish Rawat Avatar asked Sep 25 '15 09:09

Manish Rawat


People also ask

What is hub proxy SignalR?

The SignalR Hubs API enables you to make remote procedure calls (RPCs) from a server to connected clients and from clients to the server. In server code, you define methods that can be called by clients, and you call methods that run on the client.

What is hubName in SignalR?

SignalR Hubs are a way to logically group connections as documented. Take an example of a chat application. A group of users could be part of the same hub which allows for sending messages between users in the group. The hubName here can be any string which is used to scope messages being sent between clients.

What is Hub class SignalR?

A Hub is a class used for the communication. In javascript you can use a hub like this: $(function() { var myConnection = $.connection.myHub; $.connection.hub.start(); }); In ASP.NET you do this: public class Chat : Hub { public void Distribute(string message) { Clients.receive(Caller.name, message); } }

What is SignalR connection?

SignalR handles connection management automatically, and lets you broadcast messages to all connected clients simultaneously, like a chat room. You can also send messages to specific clients.


1 Answers

I tested it by putting debug point on both hubs, and the OnConnected does get invoked on both hubs, as long as you have any subscriptions to both hubs.

See here: Can I debug OnConnected method in SignalR Hub?

Long story short: By design, if you don't have any subscriptions to the hub, then the javascript client can't get any messages from server so the OnConnected won't get called.

EDIT

See here the Note part:

Note: For JavaScript clients you have to register at least one event handler before calling the Start method to establish the connection.

See more at the link.

like image 108
DDan Avatar answered Sep 19 '22 15:09

DDan