controlling the heartbeat timeout from the client in socket io

controlling the heartbeat timeout from the client in socket io  using -'node.js,client,'

I have mobile clients connected to a node.js server, running via xhr-polling. I have two type of clients:

Type A

When the connection breaks up due to network problems (or that the
client craches) the default heart beat timout is to loong
Type B

When the connection breaks up for this client i need to give it more
time to recover - it is more important that the client recover than
that the server breaks the connection/session

So my question is how to I configure (if it is possible) the heart beat timeouts from the actual client?

asked Sep 18, 2015 by virendra.bajaj
0 votes

3 Answers

0 votes

As far as I can tell, there are 2 values that matter here: the server sends heartbeats to the client every heartbeat interval seconds; the client responds directly, if there is no response, the server decides the client is dead. The client waits for a heartbeat from the server for heartbeat timeout seconds since the last heartbeat (which should obviously be higher than the heartbeat interval). If it hasn't received word from the server in heartbeat timeout seconds, it assumes the server is dead (and will start disconnecting / reconnecting based on the other options you have set.

Default values are heartbeat interval = 25s and heartbeat timeout = 60s. Both items are set on the server, the heartbeat timeout is sent to the client upon connecting.

Changing the heartbeat timeout for a single client is easy:

var socket = io.connect(url);
socket.heartbeatTimeout = 20000; // reconnect if not received heartbeat for 20 seconds

However on the server, the heartbeat interval value seems to be part of a shared object (the Manager, which is what you get back from your var io = require("").listen(server) call), which means that it can't easily be changed for individual sockets.

I'm sure that with some hacking you should be able to make it happen, but you might break other stuff in the process...

answered Sep 18, 2015 by sachin valanju
0 votes

To complete Claude's answer, you can set these values at the server lever, with the following code

io.set('heartbeat interval', 5);
io.set('heartbeat timeout', 11);

and then you could create 2 node.js servers, one for each type of client (A and B), having 2 desired set of options (heartbeat timeout and interval).

answered Sep 18, 2015 by badhwar.rohit
0 votes

In 1.x, the API has changed but the concept remains the same.

Instead of heartbeatInterval and heartbeatTimeout, the properties are pingInterval and pingTimeout. For example,

var server = app.listen(80);
io = socketio(server,{'pingInterval': 45000});

See the documentation at, which will direct you to

answered Sep 18, 2015 by kinnari