I am using SignalR 1.1.2 with SQL as backplane, and frequently (like 70% of page loads), SignalR fires of negotiate request which suceedes (doesn't fail) and just halts and nothing happens anymore. I found out that there is some conflict with jquery.validation.js so I updated: jquery, jquery validation and jquery unobtrusive ajax to latest available versions, but it still happens,,
Other times when it works correctly I can see few XHR requests made:
negotiate
connect
ping
long polling requests further on
Here is my hub:
public class NotificationsHub : Hub
{
public override Task OnConnected()
{
Groups.Add(Context.ConnectionId, CurrentUser.UserId.ToString());
return base.OnConnected();
}
public override Task OnDisconnected()
{
Groups.Remove(Context.ConnectionId, CurrentUser.UserId.ToString());
return base.OnDisconnected();
}
}
And my client code:
$(document).ready(function () {
var nh = $.connection.notificationsHub;
nh.notify = function (notificationHtml) {
$('#notification-box').find('ul').prepend(notificationHtml);
$('#notification-bubble').text($('#notification-bubble').text() + 1);
};
$.connection.hub.start();
});
While refreshing page I get error:
The connection to http://localhost:55087/signalr/connect?transport=serverSentEvents&connectionToken=FuqvvQJaxyCfy0dj2DEoZQFkBmthBk9LxU6ZEGZO4ypbSy8pUpzrvCKWTjykxp9E9GtMdwLI0sX_tWkvK1XfJaEtjFHOL3Qmeg2eILh4pTZnEzXDyq6KPuvPw_4kzj9VtNe89YsWW-3sstPwu_I60A2&connectionData=%5B%5D&tid=4 was interrupted while the page was loading.
And then following POST request which succedes:
http://localhost:55087/signalr/abort?transport=serverSentEvents&connectionToken=FuqvvQJaxyCfy0dj2DEoZQFkBmthBk9LxU6ZEGZO4ypbSy8pUpzrvCKWTjykxp9E9GtMdwLI0sX_tWkvK1XfJaEtjFHOL3Qmeg2eILh4pTZnEzXDyq6KPuvPw_4kzj9VtNe89YsWW-3sstPwu_I60A2
Here is a quick fix, but I would like to know is this a bug or normal behaviour of SignalR:
SignalR event becomes intermittent when deployed to a server
Update 2:
Fix from above worked but long poll XHR requests lasted for few minutes, which seemed odd to me and client side was updated after few minutes when request compleated which isn't acceptable, so I decided to upgrade SignalR to 2.0.0 beta2.
After that first time I was running applications SignalR couldn't connect(timeout after second or so).. Second time everything worked fine, long poll XHR were completing in 2 sec tops. Third time back to step one, no ping, no connnect, no long poll requests. BUT when something is triggered from server side it get's updated immedietly like it is connected through websockets.. and only thing I can see in debug window is:
[00:12:52 GMT+0200 (Central European Standard Time)] SignalR: Triggering client hub event 'notify' on hub 'NotificationsHub'.
Here are my changes to Hub which work with SignalR 2.0.0 beta2
public class NotificationsHub : Hub
{
public async override Task OnConnected()
{
var user = WebSecurity.GetUserId(Context.User.Identity.Name);
Groups.Add(Context.ConnectionId, user.ToString());
await base.OnConnected();
}
// OnDisconnected() was dropped thanks to N. Taylor Mullen
}
Client side:
$(document).ready(function () {
$.connection.notificationsHub.client.notify = function (notificationHtml) {
$('#notification-box').find('ul').prepend(notificationHtml);
$('#notification-bubble').text(parseInt($('#notification-bubble').text()) + 1);
};
$.connection.hub.logging = true; // optional, if you want to see what's going on
$.connection.hub.start();
});
Your connection interruption should be resolved by a reconnect event. However that doesn't seem to be happening. You have some issues in your Hub, modify it to be: