15

I can't seem to find out how to set the heartbeat options for socket.io? I'd love to be able to specify what happens when a "timemout" happens, and when the server hasn't gotten any response from the client for some time.

For some reason the disconnect event isn't always firing, even though it has passed over an hour without the client not being connected. This often happens with the standalone Java socket.io client

2 Answers2

24

Socket.IO uses engine.io, which allows you to specify ping interval and ping timeout, like so:

var app = require('express')();
var http = require('http').Server(app);

var io = require('socket.io')(http, {'pingInterval': 2000, 'pingTimeout': 5000});

(2 second interval and 5 second timeout is just an example. It might be to frequent for your use case)

Tholle
  • 108,070
  • 19
  • 198
  • 189
  • 2
    how to determine most effective interval and timeout? default 25 and 60 seconds looks too long for me or they are ok? – alexey.metelkin Feb 08 '16 at 11:19
  • @alexey.hippie It's hard to give a recommendation for every situation :) You could experiment with a much lower values, and if you have performance issues, bring them up a bit. I don't see a reason to change it if you really don't have to. – Tholle Feb 08 '16 at 13:48
  • Thanks, will keep trying :) – alexey.metelkin Feb 08 '16 at 14:27
  • 1
    Would a longer interval between heartbeats reduce mobile battery usage whilst connected, or is the difference negligible? And I wonder what risks there are with having a longer interval. – George Dec 05 '16 at 17:36
11

For me the following worked:

var app = express(); 
var server = require('http').createServer(app); 
var io = require('socket.io')(server); 

io.set('heartbeat timeout', 4000); 
io.set('heartbeat interval', 2000);

For package dependency versions:

"express": "^4.12.3",
"socket.io": "1.0"
WiRa
  • 949
  • 10
  • 14
  • 4000 = 4 second or 4 minute? –  Oct 04 '17 at 13:19
  • 1
    The parameter is milliseconds, so 4000 = 4 seconds (yes that is a very short period, but was required in my case...) – WiRa Oct 05 '17 at 12:46
  • What exactly the interval 2 second and timeout 4 second does? A) if 4 second no connectivity then timeout B) interval means every 2 second check the connectivity is alive or not. right? –  Oct 05 '17 at 20:05
  • Can you have heartbeat event? like `socket.on('heartbeat', function(msg) { // do some tasks ? } );` –  Oct 05 '17 at 20:08
  • `io.set('heartbeat timeout' , 32400000); ` what happen when i set this? i wanted 9 hour. But doing so will it apply my server in slow motion? –  Oct 05 '17 at 20:52
  • 1
    YumYumYum, I thing that you want just an interval (use setInterval) instead of a heartbeat event? The heartbeat is there to detect that websockets are still alive, by polling the connection. Typically you do not want to set this to long times! Setting it to 9h is pointless: a socket connects, connection fails for example after 30min... you won't know for 8.5h that the connection cannot be used anymore! – WiRa Oct 10 '17 at 09:35
  • Thats the problem. When you use 'heartbeat timeout' with lesser value the problem i noticed. it keeps disconnect and then reconnect. In that case its overkilling, so i needed higher value to have official hours –  Oct 11 '17 at 06:21
  • 1
    When the internal mechanism detects that your socket connection is lost you must start investigating why the connection gets lost, not increase the interval! The fact that the server thinks a client is still online does not mean that is actually is still online... I used a couple of seconds because I needed near realtime information and connectivity updates. – WiRa Oct 11 '17 at 13:11
  • Well, when i applied your theory. It failed cause in server it was doing 'disconnect' and 'reconnect' to make the `heartbeat interval` process. which was not acceptable in my. i spend like 48hours checking the logs and i was correct when you use low value, its not doing proper checkup but its manually breaking the connection and then having reconnection. –  Oct 12 '17 at 05:36
  • Then you should search for issues with automatic disconnecting/reconnecting. The heartbeat does not disconnect clients, it only checks if the communication channel is still open, if not then it tells you that the socket disconnected. – WiRa Oct 12 '17 at 09:45