node.js , socket.io - disconnect event delay when internet connection dies - node.js

I notice that socket.on('disconnect') fires immediately when I close browser tab. But it fires approximately in a minute after internet connection dies. I'm using socket.io module. How can I fix this issue?

You have to use pingTimeout and pingInterval options on the server side to control this:
const io = require('socket.io').listen(server, {
pingTimeout: 5000,
pingInterval: 10000
});
pingTimeout (Number): how many ms without a pong packet to consider the connection closed (5000)
pingInterval (Number): how many ms before sending a new ping packet (25000)
More details about params: https://github.com/socketio/engine.io#methods-1

Related

Socket.IO not getting disconnect event on polling

My browser is defaulting to "polling" method, causing me not to get the disconnect event on the server side.
I've tried the solution covered in socket.io force a disconnect over XHR-polling but this didn't do the trick for me:
Server.socket = io.connect("https://somedomain:8443", {"sync disconnect on unload":true, secure:true});
How can I track users leaving my server with polling?
so, apparently for some reason the heartbeat timeout was too long for me to think the disconnect mechanism was working.
I changed the timings:
var io = require('socket.io')(server, {'pingInterval': 4000, 'pingTimeout': 8000});
In the server, and after 8 seconds, sure thing, I get the disconnect event.
The low numbers are because I'm creating a multiplayer game...

Custom ping-pong socket.io

When you open chrome dev tools, in "frames" tab of our websocket you will notice periodic data sending-receiving (called "ping" for client request and "pong" for server responses).
Well, in socket.io v.1.4.5 you would just see these:
2
3
2
3
2
3
And so on. Can I attach some data to these periodic transactions? I would like to make kinda this:
2['ping',{data:data}]
3['pong',{data:data}]
I've already found that:
For some reasons clients can't make socket.emit('ping',{}) by
itself
Server can't detect event like
socket.on('ping',function)
The point is to make those ping-pong more meaningful and to kill two birds with one stone
Just to add up on the answer posted by, oleksii-shnyra.
As per Socket.IO Docs, there are some reserved events and shouldn't be used.
If you are using socket.io-client, you can listen for 'Ping' & 'Pong' events in the client side. You would see the latency for 'Pong' event. For example the below code,
var socket = require('socket.io-client')('Some_IP_Address:PORT');
socket.on('connect', function() {
console.log("Client is Connected");
});
socket.on('pong', function(data) {
console.log('Received Pong: ', data);
});
I received the below output in the console.
Client is Connected
Received Pong: 143
Received Pong: 143
Received Pong: 148
Received Pong: 147
As per documentation, '143' is the latency in 'ms' since the 'ping' was sent from client.
Note: You can listen for 'Ping' event as well but it usually returns empty data.
However you wouldn't be able to listen to the Ping/Pong events on the server side.
I am not intimately familiar with under-the-hood workings of socketeio or websockets however I really doubt that you can extend those polling events that socketio sends. On top of that, I do think that tinkering with those would not be healthy for your socketio solution, better leave it as it is and implement your custom events as needed.
P.S. socketio does not prevent you from using 'ping' and 'pong' as your custom event names. Double check your code :)
I was looking for a way to set an interval to determine disconnection from the server or internet and this page helped me
Click me
Github docs
I used it in the backend
{
pingTimeout: 2000,
pingInterval: 3000,
}
I hope this will save someone time.

Node.JS net module handling unexpected connection loss

I can't figure out one problem I got.
I'm using the Net module on my Node.JS server which is used to listen to client connections.
The client do connect to the server correctly and the connection remains available to read/write data. So far, so good. But when the client unexpectedly disconnects (ed. when internet falls away at client side) I want to fire an event server side.
In socket.io it would be done with the 'disconnect' event, but this event doesn't seem to exist for the Net module. How is it possible to do?
I've searched on Google/StackOverflow and in the Net documentation (https://nodejs.org/api/net.html) but I couldn't find anything usefull. I'm sry if I did mis something.
Here is a code snippet I got:
var net = require('net');
var server = net.createServer(function(connection) {
console.log('client connected');
connection.wildcard = false;//Connection must be initialised with a configuration stored in the database
connection.bidirectional = true;//When piped this connection will be configured as bidirectional
connection.setKeepAlive(true, 500);
connection.setTimeout(3000);
connection.on('close', function (){
console.log('Socket is closed');
});
connection.on('error', function (err) {
console.log('An error happened in connection' + err.stack);
});
connection.on('end', function () {
console.log('Socket did disconnect');
});
connection.on('timeout', function () {
console.log('Socket did timeout');
connection.end();
});
connection.on('data', function (data) {
//Handling incoming data
});
});
serverUmrs.listen(40000, function () {
console.log('server is listening');
});
All the events(close, end, error, timeout) don't fire when I disconnect the client(by pulling out the UTP cable).
Thanks in advance!
EDIT:
I did add a timeout event in the code here above but the only thing that happens is that the socket does timeout after 3 seconds everytime the client does connect again. Isn't KeepAlive enough to make the socket not Idle? How is it possible to make the socket not idle without to much overhead. It may be possible that there are more than 10,000 connections at the same time which must remain alive as long as they are connected (ie respond to the keepalive message).
Update:
I think the KeepAlive is not related with the Idle state of socket, sort of.
Here is my test, I remove the following code in your example.
//connection.setKeepAlive(true, 500);
Then test this server with one client connect to it var nc localhost 40000. If there is no message sending to server after 3 seconds, the server logs as below
Socket did timeout
Socket did disconnect
Socket is closed
The timeout event is triggered without KeepAlive setting.
Do further investigation, refer to the Node.js code
function onread(nread, buffer) {
//...
self._unrefTimer();
We know timeout event is triggered by onread() operation of socket. Namely, if there is no read operation after 3 seconds, the timeout event will be emitted. To be more precisely, not only onread but also write successfully will call _unrefTimer().
In summary, when the write or read operation on the socket, it is NOT idle.
Actually, the close event is used to detect the client connection is alive or not, also mentioned in this SO question.
Emitted when the server closes. Note that if connections exist, this event is not emitted until all connections are ended.
However, in your case
disconnect the client(by pulling out the UTP cable).
The timeout event should be used to detective the connection inactivity. This is only to notify that the socket has been idle. The user must manually close the connection. Please refer to this question.
In TCP connection, end event fire when the client sends 'FIN' message to the server.
If the client side is not sending 'FIN' message that event is not firing.
For example, in your situation,
But when the client unexpectedly disconnects (ed. when internet falls away at client side) I want to fire an event server side.
There may not be a 'FIN' message because internet is gone.
So you should handle this situation in timeout without using keepAlive. If there is no data coming data, you should end or destroy the socket.
EDIT: I did add a timeout event in the code here above but the only
thing that happens is that the socket does timeout after 3 seconds
everytime the client does connect again. Isn't KeepAlive enough to
make the socket not Idle? How is it possible to make the socket not
idle without to much overhead. It may be possible that there are more
than 10,000 connections at the same time which must remain alive as
long as they are connected (ie respond to the keepalive message).
For your edit, your devices should send to the server some heartbeat message between a time period. So that, server understands that that device is alive and that timeout event will not fire because you get some data. If there is no heartbeat message such cases you cannot handle this problem.

socket.io client reconnect timeout

I'm using socket.io with node.js and I like the solution. The only issue I notice is around a disconnection and reconnection.
These are my current settings:
'connect timeout': 1000,
'reconnect': true,
'reconnection delay': 300,
'max reconnection attempts': 10000,
'force new connection':true
I notice if I stop and start the node.js process the client connects back fine and quickly, however if the server is offline for a couple of minutes the client either never reconnects or takes a very long (non-user friendly) amount of time to.
I wanted to ask if there is anything I've missed or could add to the socket.io configuration to keep the client polling for a reconnection.
I know 'reconnection delay':
reconnection delay defaults to 500 ms
The initial timeout to start a reconnect, this is increased using an
exponential back off algorithm each time a new reconnection attempt
has been made.
But the exponential effect its not very user friendly. Is there a way to keep checking for a connection every X period of time - eg: 5 seconds.
If not I guess I can write some client side JS to check the connect and attempt reconnections if needed but it would be nice if the socket.io client offered this.
thx
There is a configuration option, reconnection limit (see Configuring Socket.IO):
reconnection limit defaults to Infinity
The maximum reconnection delay in milliseconds, or Infinity.
It can be set as follows:
io.set("reconnection limit", 5000);
When set, the reconnection delay will continue to increase (according to the exponential back off algorithm), but only up to the maximum value you specify.
EDIT: See answer below for proper approach
I am afraid that the reconnection algorithm cannot be modified (as of December 2013); the Github issue to allow this feature is not merged yet. However, one of the commentors is suggesting a small workaround, which should nullify the exponential increase:
socket.socket.reconnectionDelay /= 2 on reconnecting
The other approach is, as you said, to write some client-side code to overwrite the reconnecting behavior, and do polling. Here is an example of how this could be done.
EDIT: the above code will have to go inside the 'disconnect' event callback:
var socket = io('http://localhost');
socket.on('connect', function(){
socket.on('disconnect', function(){
socket.socket.reconnectionDelay /= 2;
});
});
This is the solution I went with - when the socket disconnects it enters a loop that keeps trying to reconnect every 3 seconds until the connection is created - works a treat:
socket.on('disconnect', function() {
socketConnectTimeInterval = setInterval(function () {
socket.socket.reconnect();
if(socket.socket.connected) {clearInterval(socketConnectTimeInterval);}
}, 3000);
});
cheers

Socket.io "connection" event fired on every client heartbeat?

I have a basic Socket.io server setup like this:
var server = express.createServer().listen(port);
this.io = socketIO.listen(server);
this.io.sockets.on('connection', function(socket){
initSocket(socket);
});
I also have socket.io configured to use XHR polling like so:
io.set("transports", ["xhr-polling"]);
io.set("close timeout", 3);
io.set("heartbeat interval", 2);
The issue is every time the client sends a heartbeat (every 2 sec), the 'connection' event is being fired. Is there a different event I can use that will fire once each time a socket initially connects?
I would use the "authorization" event, but that only passes in a handshake object not the actual socket.
Found the problem. I had my xhr "polling duration" set to heroku's suggested 10s like so:
io.set("polling duration", 10);
Which means that the client only makes a new xhr request every 10 seconds (as soon as the previous request returns). At the same time I had the "close timeout" set to 3 seconds:
io.set("close timeout", 3);
Which means if the server does not hear from the client within 3 seconds since its last request it closes the connection, hence the continuous 'disconnect/connection' events being fired.
The solution was to set the close timeout higher than the polling duration. It would seem that the "heartbeat interval" is not relevant for xhr connections.
Are you setting too low a value for closetimeout and heartbeat interval. The default values for these are 60 sec and 25 sec. Typical network lag is of the order of 5 sec. So with the values that you have set, the client does not respond in the time specified, so the connection is closed. When the client connects again, the connection event is fired.

Resources