Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signalr (1.0.0-alpha2) Hubs - Can you add client functions after connection has been started?

Using Signalr (1.0.0-alpha2), I want to know if it is possible to add client functions after a connection has been started.

Say I create my connection and grab the proxy. Then I add some Server Fired client functions to the hub to do a few things. Then I start my connection. I then want to add some more Server Fired functions to my hub object. Is this possible?

var myHub= $.connection.myHub;
myHub.SomeClientFunction = function() {
   alert("serverside called 'Clients.SomeClientFunction()'");
};
$.connection.hub.start()
   .done(function() {
      myHub.SomeNewClientFunction = function() {
        alert("serverside called 'Clients.SomeNewClientFunction()'");
      }
    })

This example is not realistic, but I basically want to send my 'myHub' variable to a different object after the hub is started to subscribe to new events that the original code did not care for.

Real Life Example: A dashboard with a number of different hub events (new site visits, chat message, site error). I 'subscribe' after the connection has started and then pass my hub proxy to all of my different UI components to handle their specific 'message types'. Should I create separate Hubs for these or should I be able to add more Server Fired client functions on the fly?

like image 619
ncyankee Avatar asked Nov 21 '12 16:11

ncyankee


1 Answers

Yes you can. Use the .on method.

Example:

myHub.on('somethingNew', function() {
    alert("This was called after the connection started!");
});

If you want to remove it later on use the .off method.

like image 123
N. Taylor Mullen Avatar answered Oct 05 '22 06:10

N. Taylor Mullen