Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js and Socket.IO - How to reconnect as soon as disconnect happens

Tags:

node.js

I'm building a small prototype with node.js and socket.io. Everything is working well, the only issue I'm facing is that my node.js connection will disconnect and I'm forced to refresh the page in order to get the connection up and running again.

Is there a way to reestablish the connection as soon as the disconnect event is fired?

From what I've heard, this is a common issue. So, I'm looking for a best-practice approach to solving this problem :)

Thanks very much, Dan

like image 475
Dan Avatar asked Dec 13 '10 18:12

Dan


People also ask

Does Socket.IO auto reconnect?

In the first case, the Socket will automatically try to reconnect, after a given delay.

How do I reconnect a Socket.IO client?

You can reconnect by following client side config. I don't think forceNew means reconnect. I believe it means to create a new socket each time this statement is called because normally io. connect() will return the same socket if you call it a second time.

Does Socket.IO have a timeout?

So, you can configure both the server and client side timeout settings by setting the following timeout properties on the engine.io component inside of socket.io.


2 Answers

EDIT: socket.io now has built-in reconnection support. Use that.

e.g. (these are the defaults):

io.connect('http://localhost', {   'reconnection': true,   'reconnectionDelay': 500,   'reconnectionAttempts': 10 }); 

This is what I did:

socket.on('disconnect', function () {   console.log('reconnecting...')   socket.connect() }) socket.on('connect_failed', function () {   console.log('connection failed. reconnecting...')   socket.connect() }) 

It seems to work pretty well, though I've only tested it on the websocket transport.

like image 100
nornagon Avatar answered Sep 19 '22 06:09

nornagon


edit: Socket.io has builtin-support now

When I used socket.io the disconnect did not happen(only when i closed the server manually). But you could just reconnect after say for example 10 seconds on failure or something on disconnect event.

socket.on('disconnect', function(){    // reconnect }); 

I came up with the following implementation:

client-side javascript

var connected = false; const RETRY_INTERVAL = 10000; var timeout;  socket.on('connect', function() {   connected = true;   clearTimeout(timeout);   socket.send({'subscribe': 'schaftenaar'});   content.html("<b>Connected to server.</b>"); });  socket.on('disconnect', function() {   connected = false;   console.log('disconnected');   content.html("<b>Disconnected! Trying to automatically to reconnect in " +                                    RETRY_INTERVAL/1000 + " seconds.</b>");   retryConnectOnFailure(RETRY_INTERVAL); });  var retryConnectOnFailure = function(retryInMilliseconds) {     setTimeout(function() {       if (!connected) {         $.get('/ping', function(data) {           connected = true;           window.location.href = unescape(window.location.pathname);         });         retryConnectOnFailure(retryInMilliseconds);       }     }, retryInMilliseconds);   }  // start connection socket.connect(); retryConnectOnFailure(RETRY_INTERVAL); 

serverside(node.js):

// express route to ping server. app.get('/ping', function(req, res) {     res.send('pong'); }); 
like image 34
Alfred Avatar answered Sep 17 '22 06:09

Alfred