MTplus
MTplus

Reputation: 2391

SignalR 2 does not update

I have a asp.net page that use SQLDependency Asp.Net SignalR 2 to display changes from my database automatically. My problem is that I cannot see the changes on my page even if they are correct when I debug. If I update the database the page still show the old data, but my hub get the new data. But its not displayed. The only thing I got working is to reload the page through a timer like this..

<script>
    function cache_clear() {
        window.location.reload(true);
    }

    $(document).ready(function () {
        var count = 60;
        setInterval(function () {
            $("b[id=show-time]").html(count--);
            if (count == 0) cache_clear();
        }, 1000)});
</script> 

But that also make the whole idea of SqlDependency redundant. Can anyone give me a hint of what I need to do to get the updated data to be displayed on the page?

    <script>

        $(function () {
            var notify = $.connection.notificationsHub;

            notify.client.displayNotification = function (msg) {
                $("#newData").html(msg);
                $("#LitDiv").hide();
            };

            $.connection.hub.start();
        });
    </script>

private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Info == SqlNotificationInfo.Insert || e.Info == SqlNotificationInfo.Delete || e.Info == SqlNotificationInfo.Update)
            { SendNotifications(); }
}            

        public void NotfiyAllClients(string msg)
        {
            IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
            // I can see that my database changes are made here, but it does not display that on the page unless I reload the page
            context.Clients.All.displayNotification(msg);
        }    

And the SendNotification method use this...

var nHub = new NotificationsHub();
nHub.NotfiyAllClients(message);

EDIT: If I bring up the console I get this error..

JavaScript runtime error: Unable to get property 'notificationsHub' of undefined or null reference

Upvotes: 0

Views: 1924

Answers (2)

MTplus
MTplus

Reputation: 2391

I have found the solution, I had 2 references to jQuery on my page. The first was placed in the head section and was jquery-1.6.4.min, the other one was placed before the end of the body tag (jQuery v1.11.2).

After removing the one that was placed at end of the body tag (jQuery v1.11.2), it worked as excpected.

Upvotes: 1

tire0011
tire0011

Reputation: 1058

I think your hub defintion is not correct (pseudo code not 100%):

thats your hub definition:

class NotificationsHub : Hub {
    public void DisplayNotification(string msg){}
}

in the SendNotification method use:

 IHubContext context = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
 context.Clients.All.displayNotification(msg);

or use an seperate informer class not the hub

Upvotes: 0

Related Questions