SignalR has been causing some strange behavior for me lately. After doing some refactoring, I started experiencing connectivity issues. It seems like my code was just lucky to work before because it didn't follow the recommended practices. For example, I was starting the hub before declaring the hub methods, as pointed out by David Fowler, so the client wasn't explicitly subscribing to those hubs, yet somehow it still worked. I've spent all day trying to figure out why and how this happened.
After refactoring, here is the code that runs once the document is loaded:
function initSignalR() {
var me = this,
appointmentHub = $.connection.appointmentHub,
assignmentHub = $.connection.assignmentHub,
taskHub = $.connection.taskHub,
notificationHub = $.connection.notificationHub,
messageHub = $.connection.messageHub;
$.connection.hub.connectionSlow(onConnectionSlow);
$.connection.hub.stateChanged(onStateChanged);
$.connection.hub.error(onError);
$.connection.hub.disconnected(onDisconnected);
$.connection.hub.logging = true;
appointmentHub.client.updateAppointment = onUpdateAppointment;
$.connection.hub.start().done(onDone).fail(onFailed);
... code omitted for brevity ...
}
function onUpdatedAppointment(appointment) {
.... code omitted for brevity ....
}
These logs occasionally appear in the console when it works:
Client subscribed to hub 'appointmenthub'
Negotiating with '/signalr/negotiate?clientProtocol=1.5&connectionData=%5B%7B%22name%22%3A%22appointmenthub%22%7D%5D'
webSockets transport starting.
Connecting to websocket endpoint 'ws://localhost:52541/signalr/connect?....
Websocket opened.
webSockets transport connected. Initiating start request.
The start request succeeded. Transitioning to the connected state.
Now monitoring keep alive with a warning timeout of 13333.333333333332, keep alive timeout of 20000 and disconnecting timeout of 30000
I intentionally used the word 'occasionally' because SignalR only connects correctly every now and then. Most of the time, the last visible step in the console is:
webSockets transport connected. Initiating start request.
I thought the callback method body might be the issue at first. I noticed I could connect more often with an empty function, but even that didn't solve the problem. I'm running out of ideas on what to do next.
For completeness, here is the startup code in ASP.NET MVC:
GlobalHost.Configuration.TransportConnectTimeout = TimeSpan.FromSeconds(50);
GlobalHost.Configuration.ConnectionTimeout = TimeSpan.FromSeconds(110);
GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
GlobalHost.DependencyResolver.Register(typeof(IJavaScriptMinifier), () => new SignalRMinifier());
GlobalHost.DependencyResolver.Register(typeof(IUserIdProvider), () => new UserIdProvider());
HubConfiguration hubConfiguration = new HubConfiguration()
{
EnableDetailedErrors = true,
};
return app.MapSignalR(hubConfiguration);
I came across a similar issue but no solution was provided there.
I have tested this in IE/Edge/Chrome/Firefox/Opera, and they all show the same results. The application is using ASP.NET MVC5 and the latest version of SignalR (2.2.1).