Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io Random Disconnects (v1.0.6)

I am using the latest version of socket.io (1.0.6) to make an online multiplayer game with Phaser and Node. My problem is that once the clients have connected, they will occasionally and at random, disconnect. There does not seem to be a specific case in which this happens. Sometimes it is while the game is completely idle, other times it is while all players are sending inputs to the server.

By checking the debug output from socket.io, I found that the reason for the disconnects is a "ping timeout". Specifically, the following line is being fired from the socket.js library file:

Socket.prototype.setPingTimeout = function () {
  var self = this;
  clearTimeout(self.pingTimeoutTimer);
  self.pingTimeoutTimer = setTimeout(function () {
    self.onClose('ping timeout'); // <------------------------
  }, self.server.pingInterval + self.server.pingTimeout);
};

Is there a reason this would be happening? I am just testing my server over localhost, so I have no reason to think there would be any significant delay to cause a timeout. My sockets are set up in line with the chat app example on socket.io's website:

Server:

//http server setup
var io = require('socket.io')(http);
io.on('connection', function(socket){
  //Game logic,socket listeners, io.emits
});

Client:

var socket = io();
//client side listeners, emissions back to server

My question is firstly what are the possible reasons I would be getting a ping timeout intermittently? and secondly, is there any way for me to set the timeout time much longer / shorter to test out how this affects the frequency of disconnects I am getting?

like image 850
Craig Innes Avatar asked Aug 20 '14 10:08

Craig Innes


People also ask

Should I use Socket.IO in 2022?

With nearly 55k stars on GitHub and about 3 million downloads on npm weekly, Socket.IO is a great library to keep an eye on in 2022. The documentation is very straightforward, meaning even an inexperienced developer should be able to get started in little to no time.

Does Socket.IO disconnect after some time?

Yep, I've had same issue actually. But I managed to solve it differently. There was no issue on code side, it was sever configuration issue where it was hosted.


1 Answers

Unfortunately you can't modify the ping intervals using socket.io, you could if you used the core library (engine.io).


Thanks to Paweł Wszoła for pointing out the correct answer. According to the docs on socket.io:

The same options passed to socket.io are always passed to the engine.io Server that gets created.

So you can set engine's ping timeout and interval by passing them as parameters.

require('socket.io').listen(app, { pingTimeout: 4000, pingInterval: 4000 });

like image 139
dscastillo171 Avatar answered Sep 18 '22 16:09

dscastillo171