SignalR not broadcasting data to clients after page refresh

3.3k views Asked by At

I have created a HUB using SignalR in which I subscribes stats and receives response from the Server. When I login for the first time I receives all stats on client side that I subscribed at login using SignalR Hub.

It continuously broadcasting the updated stats to client side over the SignalR HUB until I don't refresh the page. Once I refresh the page, the SignalR stops broadcasting updated stats to the client side.

While debugging I can see that the data is in _jsonStatsData variable and it is the data to hub context to broadcast it to the client but somehow it is not broadcasting and I gets null in console.log.

Client Side:

$(function () {

    var notifications = $.connection.localServerHub;

    notifications.client.receivedUpdatedStats = function (updatedStats) {

       console.log(updatedStats); 

    };  

    $.connection.hub.logging = true;

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

        // Passing the Stats value from here e.g 5
        notifications.server.subscribeStats(5);

    }).fail(function (e) {
        alert(e);
    });

}); 

HUB method and delegate:

[HubName("localServerHub")]
public class LocalServerHub : Hub
{
    private string _jsonStatsData = string.Empty;

    [HubMethodName("subscribeStats")]
    public string SubscribeStats(string statId)
    {
        lock (LoginUsers.ConnectedUsersList)
        {

            if (!_objHubClient.Subscribe)
            {
                _objHubClient.ResponseEvent += new HubClient.ResponseBO(Response_Recieved);

                _objHubClient.Subscribe = true;
            }

            objHubClient.SubscribeStatsNotifications(Convert.ToInt32(statId));

            return Clients.Caller.RecievedUpdatedStats(_jsonStatsData).ToString();
        }
    }

    private void Response_Recieved(Messages objMessage)
    {
        try
        {
            if (objMessage.OperationType == Operation.StatsUpdateNotification)
            {
                _jsonStatsData = JsonConvert.SerializeObject(objMessage.ResponseStats);
                SubscribeQueueStats(string.Empty);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

What could be the problem after refreshing the page? There is no session timeout problem and no disconnect problem but still I gets nothing after refresh on client side.

1

There are 1 answers

0
Suhaib Janjua On BEST ANSWER

Here is what I had done years back. It will surely help others.

Well the reason why SignalR stops broadcasting after a page refresh was; it loses the hub context in which it has to broadcast the message to its clients.

So what I did is, I created a new reference for the hub context to broadcast the messages even after a page refresh.

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<LocalServerHub>();

return context.Clients.Caller.RecievedUpdatedStats(_jsonStatsData).ToString();