17

I seem to have an issue with SignalR's JS Client Hub.

The problem is that the 'on' handler does not seem to work - it generates no error but doesn't receive any signals sent by the server. The code below shows an extract where I call the server (using the invoke) which works fine - then on the server I call back to acceptHubData which should be picked up on the client but isn't.

My objective is when navigating to pages that each page will open a connection to a specific hub and releases this connection when the user moves to another page!!

EDIT: using the following code snippet works but I wonder why the code further below using the 'on' event doesn't work!

    var superHub = $.connection.mySuperHub;

    superHub.client.acceptHubData = function (data) {
        $('<li>hello there' + data + '</li>').prependTo($('#ul1'))
    }

    $.connection.hub.start().done(function () {
        $('<li>done phase 1</li>').prependTo($('#ul1'))
    });

Any help would be much appreciated!

This is the client code (in js)

$(document).ready(function () {

    var myHub;

    try {

        var connection = $.hubConnection();

        connection.start().done(function () {

            myHub = connection.createHubProxy("mySuperHub");

            myHub.on('acceptHubData', function (data) {
                alert(data);   // THIS IS NOT CALLED!
            });

            myHub.invoke('AcceptSignal', "hello from the client2");

        });

    }
    catch (e) {
        alert(e.message);
    }
});

This is the Server code:

[HubName("mySuperHub")]
public class MyHub : Hub
{

    private readonly HubEngine _hubEngine;

    public MyHub() : this(HubEngine.Instance) { }

    public MyHub(HubEngine hubEngine)
    {
        _hubEngine = hubEngine;
    }

    public void AcceptSignal(string msg)
    {
        Clients.Caller.acceptHubData("hi");
        Clients.All.acceptHubData("hi");
    }

}
Marcel
  • 2,148
  • 6
  • 31
  • 48

2 Answers2

46

You can still use the on method to add events for JS client hub method calls in the latest version of SignalR, but if you do not add any event listeners to a hubProxy before calling hubConnection.start(), you will not be subscribed to the hub. SignalR subscribes to the hubs you have event handlers for when the hubConnection starts. If you are not subscribed to your hub, adding any events to that hub after start() won't work.

If you add at least one event listener to the hub before start(), even if it doesn't do anything, you can then add any additional event handlers you want to the hub using on after start() and your handlers will be called.

It doesn't matter if you add an event using hubProxy.on('eventName', function (... or autogeneratedHubProxy.client.eventName = function (... before you call start(), but only on will successfully add event listeners after start() is called.

halter73
  • 15,059
  • 3
  • 49
  • 60
  • 1
    Fantastic - that works indeed - would not have guessed that! I have tested it by adding a non-existent event and that also works! Thanks – Marcel Apr 17 '13 at 20:10
  • Man, I was tearing my hair out (what little I had left) trying to figure out what I did wrong. It seemed backwards to subscribe before you called start, but apparently that's what you need to do. FYI - I was using the `on()` method and it wouldn't work if subscribing after `start()`. – Zach Apr 27 '16 at 12:55
  • I was scouring the web for what could be causing my listeners not to work, and found this. Thanks a lot!! Adding a dummy event handler right before start on the hub connection is called fixed it, even if all the other event handlers occur after start. -Mike – mike gold Aug 02 '20 at 15:14
1

Not sure which version of SignalR you are using, but I have had more success using the following syntax on my server:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.acceptHubData("hello");    

and on my clients:

myHub.client.acceptHubData = function (data) {
    console.log(data);
}
paul
  • 21,653
  • 1
  • 53
  • 54
  • thanks for the response - that is working indeed - I would like to understand why the 'on' event principle isn't working - see my edit! – Marcel Apr 17 '13 at 16:05
  • potentially you might be looking at documentation for an older version of signalr. Certainly, this is the approach recommended in the documentation here: https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs – paul Apr 17 '13 at 16:10
  • I was looking at this one https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs-%28No-Proxy%29 – Marcel Apr 17 '13 at 16:12