Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR with multiple event handlers per event

In C# i would hook many handlers to an event like so:

event += firstEventHandler;
event += secondEventHandler;

but JavaScript with SignalR we write:

$.connection.someHubName.client.someEventName = function (item) {
    console.log("someMessage", item);
};

I have created a wrapper that looks a little like this:

var signalRClient = {
    start: function (callback) {
        $.connection.hub.url = ajaxHttp + "/signalr";
        $.connection.hub.logging = true;
        if ($.connection.hub && $.connection.hub.state === $.signalR.connectionState.disconnected) {
            $.connection.hub.start({ jsonp: true, withCredentials: true, transport: ['webSockets', 'longPolling'] }).done(callback);
        } else {
            callback();
        }
    },
    connection: $.connection
}

That i can call in this way:

signalRClient.connection.somehub.client.someEvent = function (item) {
    console.log("someMessage", item);
};

signalRClient.start(function () { 
    signalRClient.connection.somehub.server.subscribe(someId);
});

but i if i run the signalRClient in more than one view (on the same rendered page) it can only assign one function to one event. I want to be able to assign many functions to one event.

I have read: connect to signalr hub and SignalR client with multiple connections ++

But i don't think it's a good idea to create many connections to the same hub for this purpose. I would also try to avoid creating my SignalR code in a layout and make all views dependent on this.

Is there a simple solution available so that i can hook on many events handlers to the $.connection.somehub.client.event ?

like image 556
Bjørn Avatar asked Apr 06 '16 08:04

Bjørn


People also ask

Can you have multiple event handlers?

Only one event handler can be assigned for every event in an element. If needed the handler can be replaced by assigning another function to the same property. Below we show how to set a simple greet() function for the click event using the onclick property.

How many connections can SignalR handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

How long do SignalR connections stay open?

The default keepalive timeout period is currently 20 seconds. If your client code tries to call a Hub method while SignalR is in reconnecting mode, SignalR will try to send the command.

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.


1 Answers

After trying many different things i managed to use the .on( command properly.. it is done like this:

signalRClient.connection.somehub.on("someevent", function(item) {
    console.log(item);
});

signalRClient.connection.somehub.on("someevent", function (item) {
    console.log(item);
});

This will provide two lines in the console for each event.

like image 61
Bjørn Avatar answered Oct 06 '22 18:10

Bjørn