Whit Waldo
Whit Waldo

Reputation: 5207

SignalR server doesn't consistently call methods on client

I have an AngularJS application that I intend to have receive communications via SignalR from the server, most notably when data changes and I want the client to refresh itself.

The following is my hub logic:

[HubName("update")]
    public class SignalRHub : Hub
    {
        public static void SendDataChangedMessage(string changeType)
        {
            var context = GlobalHost.ConnectionManager.GetHubContext<SignalRHub>();
            context.Clients.All.ReceiveDataChangedMessage(changeType);
        }
    }

I use the following within my API after the data operation has successfully occurred to send the message to the clients:

SignalRHub.SendDataChangedMessage("newdata");

Within my AngularJS application, I create a service for SignalR with the following javascript that's referenced in the HTML page:

angular.module('MyApp').value('signalr', $.connection.update);

Within the root for the AngularJS module, I set this up with the following so that it starts and I can see the debug output:

$(function () {
    $.connection.hub.logging = true;
    $.connection.hub.start();
});

$.connection.hub.error(function(err) {
    console.log('An error occurred: ' + err);
});

Then I've got my controller. It's got all sorts of wonderful things in it, but I'll show the basics as relate to this issue:

angular.module('MyApp').controller('MyController', function($scope, signalr) {
  signalr.client.ReceiveDataChangedMessage = function dataReceived(changeType) {
    console.log('DataChangedUpdate: ' + changeType);
  };
});

Unfortunately, when I set a breakpoint in the javascript, this never executes though the rest of the program works fine (including performing the operation in the API).

Some additional (hopefully) helpful information:

I appreciate any help you can provide. Thanks!

Upvotes: 2

Views: 1622

Answers (1)

Wasp
Wasp

Reputation: 3425

It's not easy to reproduce it here, but it's likely that the controller function is invoked after the start of the connection. You can verify with a couple of breakpoints on the first line of the controller and on the start call. If I'm right, that's why you are not called back, because the callback on the client member must be defined before starting the connection. Try restructuring your code a bit in order to ensure the right order.

Upvotes: 1

Related Questions