SignalR not broadcasting

Go To StackoverFlow.com

2

I'm trying to create a server to client broadcast mechanism with SignalR and it doesnt seem to do anything.

I have a hub like this:

public class DataMessageService : Hub, IClientNotificationService
{
    dynamic _clients;

    public DataMessageService(IConnectionManager connectionManager)
    {
        _clients = connectionManager.GetClients<DataMessageService>();
    }

    public void SendDataChangeNotification(string entityName, string changeType, string id)
    {
        _clients.dataChangeNotification(new string[] {entityName, changeType, id});
    }
}

My _Layouts.cshtml has this:

var _centralHub;

$(function() {
    // startup the signalr hub and get the proxies we need
    _centralHub = $.connection.dataMessageService;
    $.connection.hub.start();
});

And I have some code in a partial which is loaded by a jquery tab using ajax:

_centralHub.dataChangeNotification = function (data) {
    alert(data[0] + "::" + data[1] + "::" + data[2]);
    if (data[0] == 'User') {
        grid.refresh();
    }
};

Now in the data layer, when some crud action occurs, I call DataMessageService.SendDataChangeNotification but nothing happens at the client end.

Am I missing something?

Update: I thought it might be something to do with the vs web server thingy but it also fails when using full IIS (on Win 7).

Another Update: I had confused my service with my hub. I'v'e now split these so it looks like the following, but it still doesnt work.

  public class DataMessageService : IClientNotificationService
  {
    public void SendDataChangeNotification(string entityName, string changeType, string id)
    {
      IConnectionManager icm = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
      dynamic clients = icm.GetClients<DataMessageHub>();      
      clients.dataChangeNotification(new string[] { entityName, changeType, id });
    }
  }

  public class DataMessageHub : Hub
  {
  }

:(

Even more info:

This works with FireFox but not with IE or Chrome.

I also tried to create a simple sample app and this worked fine with Chrome and IE.

Given that we don't have web sockets available to us, long polling may not be a good idea for our users/infrastructure. Maybe one day...

2012-04-03 20:35
by Jonesie
Thanks for the edit Mike :) I couldn't get the code to format nicely - Jonesie 2012-04-03 20:43
I've had similar strange issues with SignalR. Calling a dummy server-side method from the JS, just after start was the only way for me to get server-side calls to the client to work. '$.connection.hub.start(function() { hubName.dummyMethodOnServer(); }) - Svend 2012-04-15 14:08
Hey, thanks for this. I made the change you suggested and it worked... once, then stopped. :( Not sure why yet, but there is hope - Jonesie 2012-04-18 02:21


0

A new instance of the hub is created every time it is resolved so you can't persist state like that.

You can get all clients in the hub from this.Clients.

To broadcast from outside of the hub class use this:

IConnectionManager connectionManager = AspNetHost.DependencyResolver.Resolve<IConnectionManager>();
dynamic clients = connectionManager.GetClients<DataMessageService>();
clients.dataChangeNotification(new string[] {entityName, changeType, id});
2012-04-04 01:42
by drch
Thanks. However, I did have exactly that and Clients was null - Jonesie 2012-04-04 01:55
Updated. Misread about where the events were coming from - drch 2012-04-04 01:59
Thanks again, but it still wont work. See my update above - Jonesie 2012-04-04 02:31