locked
signalr-client-methods-not-called-consistently-when-deployed-to-azure and in local sometimes RRS feed

  • Question

  • User1110648948 posted

    We hosted asp.net web api in azure.

    our front end application is SPA(angularjs + Asp.net MVC)

    we are using signalR for realtime updates. we are having multiple hubs in a single connection. The problem is that the clients methods are not called consistently every time.

    we have three applications

    1. UI

    2.Web.Api

    3. Agent(Windows service)

    The agent with predefined frequency calling web.api and saying it is able to connect web.api. so web.api pushing this "connected" info to all clients.

    Similarly the user also can trigger a task from UI which will eventually reach agent through web.api. There are two events for a hub "task" called taskadded and taskupdated.

    The problem is sometimes taskadded method(in UI) is called from server(web.api) but taskupdated method is not called from server. But "connected" method is called from server.

    some hub methods on UI are getting called sometimes but sometimes not.

    Code from  UI

    services.factory('signalRHub', [
    '$http', '$rootScope', '$timeout', 'urlConstants', function ($http, $rootScope, $timeout, urlConstants) {

    $.connection.hub.url = urlConstants.baseUrl + 'signalr';
    var hub1 = $.connection.hub1;
    var tasksHub = $.connection.tasks;
    var hub2 = $.connection.hub2;
    var hub3 = $.connection.hub3;
    var agentsHub = $.connection.agents;
    var hub4 = $.connection.hub4;

    tasksHub.client.taskAdded = function (task) {
    $rootScope.$broadcast('taskAdded', task);
    };

    tasksHub.client.taskUpdated = function (task) {
    $rootScope.$broadcast('taskUpdated', task);
    };

    hub1.client.method1 = function (xxxxxx) {
    $rootScope.$broadcast('method1', xxxxxx);
    };

    hub1.client.method2 = function (xxxxxxx) {
    $rootScope.$broadcast('method2', xxxxxxx);
    };

    hub2.client.method1 = function (xxxxx) {
    $rootScope.$broadcast('method1', xxxxxx);
    };

    hub2.client.method2 = function (xxxxxx) {
    $rootScope.$broadcast('method2', xxxxxx);
    };

    hub3.client.method1 = function (xxxxxxx) {
    $rootScope.$broadcast('method1', xxxxxxx);
    };

    agentsHub.client.agentConnected = function(serverKey) {
    $rootScope.$broadcast('agentConnected', serverKey);
    };

    hub4.client.method1 = function (info) {
    $rootScope.$broadcast('method1', info);
    };

    hub4.client.method2 = function (info) {
    $rootScope.$broadcast('method2', info);
    };

    $.connection.hub.reconnecting(function () {
    console.log("signlar reconnecting");
    });

    $.connection.hub.reconnected(function () {
    console.log("signalr re-connected");
    });

    $.connection.hub.disconnected(function () {
    console.log("signalr disconnected");
    $timeout(startHub, urlConstants.signalrRetryConnectTimeInterval);
    });

    var startHub = function () {
    $.connection.hub.start()
    .done(function () {
    console.log("Transport = " + $.connection.hub.transport.name);
    console.log("Connection id = " + $.connection.hub.id);
    })
    .fail(function () {
    console.log("Could not connect !");
    });
    };

    startHub();

    return {};
    }
    ]);

    public abstract class HubBase<T> where T : IHub

    {
    private readonly Lazy<IHubContext> _hub = new Lazy<IHubContext>(
    () => GlobalHost.ConnectionManager.GetHubContext<T>()
    );

    public IHubContext Hub => _hub.Value;
    }

    using Microsoft.AspNet.SignalR;
    using Microsoft.AspNet.SignalR.Hubs;

    namespace Project.Api.Hubs
    {
    [HubName("agents")]
    public class AgentsHub : Hub
    {
    }

    [HubName("hub1")]
    public class hub1: Hub
    {
    }

    [HubName("tasks")]
    public class TasksHub : Hub
    {
    }

    [HubName("hub2")]
    public class hub2: Hub
    {
    }

    [HubName("hub3")]
    public class hub3: Hub
    {
    }

    [HubName("hub4")]
    public class hub4: Hub
    {
    }

    }

    [RoutePrefix("api/tasks")]
    public class TasksController : BaseApiControllerWithHub<TasksHub>
    {

    public async Task<IHttpActionResult> AddStatus(Guid taskKey, AgentTaskStatus taskStatus)
    {
    Hub.Clients.All.taskUpdated(taskStatus);
    return Ok(taskStatus);
    }

    public async Task<IHttpActionResult> Post(AgentTask task)
    {
    Hub.Clients.All.taskAdded(task);
    return Ok();
    }

    }

    Tuesday, August 2, 2016 8:30 PM

Answers

All replies

  • User61956409 posted

    Hi kallakuri,

    You could try to define all Hub functionality in a single class and run your application to check if same issue will appear. Besides, you could enable SignalR tracing to view diagnostic information about events in SignalR app.

    http://www.asp.net/signalr/overview/testing-and-debugging/enabling-signalr-tracing

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 3, 2016 5:53 AM
  • User1110648948 posted

    Could you please explain clearly.  Do you mean not to have multiple hubs?

    Wednesday, August 3, 2016 6:51 PM
  • User61956409 posted

    Hi kallakuri,

    Could you please explain clearly.  Do you mean not to have multiple hubs?

    My meaning is you could define all Hub functionality (methods) in a single Hub class. Besides, as I mentioned in my previous reply, please enable SignalR tracing and view diagnostic information.

    Best Regards,

    Fei Han

    Friday, August 5, 2016 8:49 AM