1.3.2: client events received in browsers prior to all JS being loaded

FireFox experiences a race condition between the loading of code in scripts and receiving events if a socket establishes a connection concurrently with the loading of scripts.  If a client in FireFox connects to the server in a script, but there are socket event handlers yet to be loaded (or general JS yet to be loaded), events from the server may be received and unhandled properly on the client side due to “missing” code.  IE and Chrome tend to finish loading all JavaScript prior to handling the first event from the server, so the issue is not evident outside of FireFox.

Not documented on’s page or its GitHub page, has an “autoConnect” option on the client side initialization that helps solve this problem.  Here’s an effective connection paradigm to follow when using clients on browsers:

First, create the io Manager and initialize the socket with autoConnect = false, that way we don’t connect to the server until we set up all of our event handlers.

var socket = io('http://server_host', { autoConnect: false });</pre>
Second, set up our event handlers.
<pre class="js">socket.on('foo', function() {

Finally, after all of our JS, images, frames, etc. are loaded and we are completely ready to handle all incoming socket events, follow through with connecting to the server.

window.onload = function() {
0 thoughts on “ 1.3.2: client events received in browsers prior to all JS being loaded”

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: