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.
Related
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...
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.
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
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
I want to run some code when a a socket is closed but only if the user didn't reload the page...
I have something like
socket.on("close", function() {
//do something here
});
The problem is that this function runs during a reload event as well...Can I somehow pause it to run at a later time with the value at that later time. I tried just using a set timeout within the callback but couldn't access the socket object anymore from within the callback.
Whats the best way to prevent the function from running if a socket connection is regained shortly after?
the main concept of my thought, is that you can never know that a user is reloading or disconnecting and never come back for 1day or so , there are ways to detect that the browser is navigating away from the website, but cant know (in server side) that it will go to the same address, or different..
Instead if any clients disconnect, of course the disconnect event will be fired to socket.io server for that socket, so taking that in account you can set a Session Variable to false you can say that the player is "disconnected" so when the client of that session "reconnects" aka reloads , the socket.io will fire an "connection" event, BUT reading the session variable you can say that the client has previously disconnected and then connected again. Timestamps could apply to this so a reconnection after 15min would have to load some extra data etc..
So you can try this with sessions (assuming that you use express or something)
sockets.on("connection",function(socket){
if(session.isAReload){
// this is a reconnection
}
socket.set("isAReload",session.isAReload /*defaults to false*/);
});
sockets.on('close',function(){
socket.get('isAReload',function(err,isAReload){
if(isAReload){
// closed after reconnecting
}else{
/*
just closed connection, so next time it will be a "reload" or reconnection
a timer of lets say 30s could run to ensure that the client is reloading
*/
session.isAReload=true;
}
});
})