10

I'm trying to figure out a way for my SockJS clients to reconnect to the server if it should go down.

I currently have this:

    new_conn = function() {    
        socket = new SockJS(protocol + serverDomain + '/echo', null, {
            'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
        });
    };

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = window.setInterval(function () {
            new_conn();
        }, 2000);
    }; 

The problem is that the setInterval keeps firing even after a successful reconnect. It seems that the socket.onopen never gets executed.

Any ideas what I could be doing wrong?

franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • 3
    in your case, using setTimeout() should be better than setInterval() – lance34 Nov 10 '13 at 00:21
  • @Fielding34 Actually I think it's quite the opposite - `setInterval()` is better because OP wants to keep trying (while server is down) until it connects – TMG Jan 02 '16 at 02:23
  • @TMG The `close` event will actually be fired if a connection is attempted when the server is down, so a single `setTimeout()` is all that is necessary. – Herohtar Apr 06 '21 at 21:14

2 Answers2

10

I think it could be related to variable scoping. Try this:

var recInterval = null;

new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling']
    });
};

socket.onopen = function () {
    clearInterval(recInterval);
};  

socket.onclose = function () {    
    recInterval = window.setInterval(function () {
        new_conn();
    }, 2000);
}; 

Anyway, it's strange, because you were declaring recInterval on the window object, and it should have worked. If it doesn't work, you could also debug it with a browser, with debugger; statements or interactively by setting local breakpoints... (in onopen, for example).

By the way, I rewrited the whole code like this (I like refactoring:):

var recInterval = null;
var socket = null;

var new_conn = function() {    
    socket = new SockJS(protocol + serverDomain + '/echo', null, {
        'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming', 
                                'iframe-eventsource', 'iframe-htmlfile', 
                                'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
                                'jsonp-polling']
    });

    socket.onopen = function () {
        clearInterval(recInterval);
    };  

    socket.onclose = function () {    
        recInterval = setInterval(function () {
            new_conn();
        }, 2000);
    };
};
franzlorenzon
  • 5,845
  • 6
  • 36
  • 58
8

In case anyone still interested in this topic: The refactored code snippet from franzlorenzon causes a lot of reconnects since it is kind of recursive reconnecting itself somehow since each two seconds a new onclose event is spawned (regardless of the recInterval).

Moving the clear interval right after the socket creation does the trick. I also added a socket = null in the onclose event.

var recInterval = null;
var socket = null;

var new_conn = function() {
  socket = new SockJS(protocol + serverDomain + '/echo', null, {
    'protocols_whitelist': ['websocket', 'xdr-streaming', 'xhr-streaming',
      'iframe-eventsource', 'iframe-htmlfile',
      'xdr-polling', 'xhr-polling', 'iframe-xhr-polling',
      'jsonp-polling'
    ]
  });

  clearInterval(recInterval);

  socket.onopen = function() {

  };

  socket.onclose = function() {
    socket = null;
    recInterval = setInterval(function() {
      new_conn();
    }, 2000);
  };
};
daPhantom
  • 119
  • 2
  • 9
  • 1
    Thanks for sharing this. It might also be nice to have a back-off protocol where the time between reconnection attempts increases. Then, drop the reconnection time if the user is "frustrated" by monitoring scrolling in the app. Clearly they will trigger a reconnection attempt just by reloading the page. – coding May 05 '16 at 23:11
  • @coding Yes you are right. The above code is just an example which fixes the errors from the previous example. We're using a solution like you suggested on our production systems and it works just fine. Also I am using a similar solution for private projects. No complications as well. :) – daPhantom May 11 '16 at 14:31