SignalR connects slowly to javascript client

Sometimes it takes a second or more to connect to my SignalR server from a browser, sometimes even when working on the local computer. I thought websites should be fast!

+4
signalr
source share
2 answers

There is a configuration option to tell the SignalR JS client to wait for the page load event to complete before sending anything.

Just set waitForPageLoad: false in the startup options to prevent this from happening. Of course, you must make sure that everything you do in the callback can be safely executed if the page is not loaded.

Any video that does not load can delay the start - so I'm not sure why this is not better / more widely documented!

 $.connection.hub.start({ waitForPageLoad: false}).done(function() { }); 

Excerpt from the source code (this is how I discovered it):

  // Check to see if start is being called prior to page load // If waitForPageLoad is true we then want to re-direct function call to the window load event if (!_pageLoaded && config.waitForPageLoad === true) { connection._.deferredStartHandler = function () { connection.start(options, callback); }; _pageWindow.bind("load", connection._.deferredStartHandler); return deferred.promise(); } 
+6
source share

Another possibility: make sure that nothing blocks the browser, for example, a long initialization code.

I use knockout.js, and for some pages it has a particularly lengthy initialization - blocking the browser and creating it looks like SignalR took a few seconds, while in fact it only took a millisecond.

0
source share

All Articles