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

Using Signalr (1.0.0-alpha2), I want to know if client functions can be added after the connection has been started.

Let's say I create my connection and grab a proxy. Then I add the Server Fired client features to the hub to do a few things. Then I start my connection. Then I want to add some additional Server Fired features to my hub object. Is it 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 another object after the hub starts subscribing to new events that the source code did not care about.

Real-life example: a control panel with several different hub events (new visits to the site, chat message, site error). I sign up after the connection has started, and then pass my hub to all of my various user interface components to process their specific โ€œmessage typesโ€. Should I create separate hubs for them or should I be able to add more server related features on the fly?

+8
signalr signalr-hub
source share
2 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, use the .off method.

+14
source share

I have the same situation. You might want to add another mock abstraction if you are trying to call it from several places.

Here is a preview of what I came up with (typescript).

I will start by using. SignalRManager is my "manager" class that abstracts my debuggingHub hub. I have a client method fooChanged that runs on the server.

Somewhere in a module using SignalR, I just call the start method, which does not restart if it is already running.

 // ensure signalR is started SignalRManager.start().done(() => { $.connection.debuggingHub.server.init(); }); 

Your โ€œmoduleโ€ simply registers a callback through the manager class and whenever your SignalR client method is called, your handler is called.

 // handler for foo changed SignalRManager.onFooChanged((guid: string) => { if (this.currentSession().guid == guid) { alert('changed'); } }); 

This is a simple version of SignalRManager that uses jQuery $.Callbacks to send a query to as many modules as you have. Of course, you can use any mechanism that you need, but this is perhaps the easiest.

 module RR { export class SignalRManager { // the original promise returned when calling hub.Start static _start: JQueryPromise<any>; private static _fooChangedCallback = $.Callbacks(); // add callback for 'fooChanged' callback static onfooChanged(callback: (guid: string) => any) { SignalRManager._fooChangedCallback.add(callback); } static start(): JQueryPromise<any> { if (!SignalRManager._start) { // callback for fooChanged $.connection.debuggingHub.client.fooChanged = (guid: string) => { console.log('foo Changed ' + guid); SignalRManager._fooChangedCallback.fire.apply(arguments); }; // start hub and save the promise returned SignalRManager._start = $.connection.hub.start().done(() => { console.log('Signal R initialized'); }); } return SignalRManager._start; } } } 

Note. Additional work may be required to fix outages or lost connections.

0
source share

All Articles