Two websocket SockJS connections made on the same page

2,285 views
Skip to first unread message

Chris

unread,
Jun 17, 2013, 7:23:00 PM6/17/13
to soc...@googlegroups.com

I've observed in my application server log that, in a space of 15 seconds, two live connections are made from the same client (from the same page, i'm sure of this because of the design of my app) and message are sent using both these connections. Both connections use websocket protocols. I have two questions.

first, is this even possible? I thought sockjs only allow one live connection per page.

second, based on my front-end javascript logic, it seems impossible that two live connections can be established:

var conn = null;
var retry = null;
...

function connect() {
    disconnect();

    var options = ...
    conn = new SockJS("http://example.com/sockjs", null, options);

    conn.onopen = function() {
        ...
    };

    conn.onmessage = function(e) {
        ...
    };
  
        conn.onclose = function() {
        conn = null;
   if (...) {
       if (retry !== null) {
           window.clearTimeout(retry);
           retry = null;
       }
       retry = window.setTimeout(connect, 3000));
   }
   else if (...) {
       window.alert("connection problem!");
   }  
} 
}

function disconnect() {
    if (conn !== null) {
        conn.close();
        conn = null;
    }
}

Given the call to disconnect() at the beginning of connect() and how re-connection works in onclose callback, there should be only one connection made at any point of time. However, the server-side log clearly shows that two websocket connections are made from the same page within 15 seconds - one is made while the other is still open.

what is going on here?! 

Fugiman

unread,
Jun 17, 2013, 8:48:25 PM6/17/13
to soc...@googlegroups.com
SockJS does not prevent multiple connections in any part of the client library. It only cautions not to use two connections as non-websocket fallbacks use two http connections, which in older browsers is the maximum number of connections to a given subdomain. I believe newer browsers allow up to 6 concurrent http connections per subdomain, and unlimited websocket connections, so that shouldn't be an issue for you.

Regarding your reconnection code, I don't see anything obviously wrong. Do be warned that it's possible for conn.close() to return false and not actually disconnect (for instance, if the connection isn't in a connecting or open state). A working example of the error would be required for me to help you more.

Chris

unread,
Jun 18, 2013, 4:38:35 AM6/18/13
to soc...@googlegroups.com

That is pretty much the relevant code which produces the observed behavior. i don't think what happen in onopen or onmessage have effect on connection/disconnection behavior.

Thanks for pointing out the potential bug in conn.close(), but given how the re-connection works in the code above, the client should connect only when the connection breaks, that is, inside the onclose. So how is it possible to have two live connections going on for the same client?

Javascript is generally considered as single-threaded, but it seems like some race-condition is going on here...

Fugiman

unread,
Jun 18, 2013, 11:53:20 AM6/18/13
to soc...@googlegroups.com
As I said, there is nothing I see in your code that gives an obvious answer. However, since it is impossible to actually run your code, I can't test it myself. If you provide code for the client and server that consistently reproduces the issue, preferably with any extraneous application logic cut out, then we can test it ourselves and figure out the issue.

From what you've given me I can only assume that you call connect() outside of onclose somewhere else, and that happens to get called twice. Or the log is wrong and you've only got one connection at a time. Again, since I have no runnable code I can't tell.

ufuka...@gmail.com

unread,
Aug 2, 2013, 11:56:35 AM8/2/13
to soc...@googlegroups.com
Hi Chris,

I had the same issue, and I found out that it was due to using Chrome's prerender feature :) Once prerendered page loads Js executes on the prerendered page so it opens two webconnections.

To solve it: 
        if(!document.webkitHidden)
        {
Reply all
Reply to author
Forward
0 new messages