0

I have a notifications button on my page which indicates the number of notifications that are currently pending for a user.

I thought that I would be right to use SignalR for this, as I wish to open a connection, periodically check for an update to this notification count and then update the DOM.

I expected that I could do the following using SignalR, but I don't know how to periodically check for changes.

I thought that I had it right with the following code:

SignalR Hub

[Authorize]
public class NotificationHub : Hub
{
    private IQuizEntities DbContext;

    public NotificationHub(IQuizEntities dbContext)
    {
        this.DbContext = dbContext;
    }

    public void GetNotificationCount()
    {

        int userId = Context.User.Identity.GetUserId<int>();

        // Get the notification count for the logged in user
        int notificationCount = (from notification in this.DbContext.EntitySet<Notification>()
                                where notification.UserId == userId
                                select notification).Count();

        // If there are any notificatoins, inform the client
        if (notificationCount > 0)
        {
            Clients.Caller.updateNotificationCount(notificationCount);
        }

    }
}

Javascript:

function NotificationHeartbeat() {

    var notificationHubProxy = $.connection.notificationHub;

    notificationHubProxy.client.updateNotificationCount = function (notificationCount) {

        $('span#notification_count').html(notificationCount);

        // Change colour of button
        if (notificationCount < 1) {
            $('#notification_button').removeClass('notification_button_active');
        } else {
            $('#notification_button').addClass('notification_button_active');
        }
    };

    // Start the connection
    $.connection.hub.start().done(function () {

        // Do something

    });
}

But sadly it never updates the DOM... I thought that SignalR could be used to long-poll the server periodically?

I notice that the difference between my use case and the other one's that I've seen, is that SignalR sends out signals when a request comes along from another user doing something. I don't want to do that, I just want my server to periodically check for the number of notifications, just like any other rest service. Maybe I should just be using long polling in ajax.

Luke
  • 22,826
  • 31
  • 110
  • 193
  • 2
    While SignalR can used for polling, that is **not** the way it should be used. You should push updates from your server to the client. SignalR was specifically created to get rid of the need for polling so that servers could push updates in near real time to the clients. In this case, rather than polling for the notification count, you should *push* the count from the server to the client *only when* the count changes. Under the hood, SignalR *may* use polling, web sockets, or any of several other technologies to get the updates from the server. But that's abstracted out, don't worry about it. – mason Dec 12 '14 at 15:18
  • So there has to be some 'event'? Can this be from the server and not just called in the browser by javascript? – Luke Dec 12 '14 at 15:21
  • I guess that I need to do something like this then... http://stackoverflow.com/a/11098832/894792 ? – Luke Dec 12 '14 at 15:22
  • It *should* be from the server. Think of it as *when* the data changes, your server should *call* some client side functionality directly. And the way it does that is via SignalR. Go learn some more about it. Start with Microsoft's [site](http://www.asp.net/signalr), and also watch some YouTube videos so you can see it in action. – mason Dec 12 '14 at 15:23
  • Yes, that question and the accepted answer explain how you would embed the call in your controller action method *where the data changes* to update the client, rather than putting the call in a hub. – mason Dec 12 '14 at 15:25
  • Great stuff, thanks for clarifying this for me. I didn't realise that you could call the hubs from the server, the Microsoft tutorials that I've read made it all out to be driven from the client side. – Luke Dec 12 '14 at 15:40

0 Answers0