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?
Asked
Active
Viewed 5,811 times
3

Paramore
- 1,313
- 2
- 19
- 37
-
this is by design, heartbeat timeout is set to 60 seconds https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO#wiki-server – dark_ruby Jan 30 '14 at 13:30
-
See http://stackoverflow.com/a/12846918/1221252 – leszek.hanusz Jan 30 '14 at 13:32
-
Should I set heartbeat timeout to 1 second? Does it have some negative sides? – Paramore Jan 30 '14 at 13:39
-
What do you want to fix in it? I mean do you want your client to emit disconnect event the very moment it loses internet connection? – M Omayr Jan 31 '14 at 08:07
1 Answers
13
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

Footniko
- 2,682
- 2
- 27
- 36
-
great! how is it connected with engine.io? isn't engine.io built over socket.io? i can't find any documentation for socket.io. again thanks, that helped me very much! – Dima Gimburg Sep 16 '15 at 11:29
-
1You welcome. Engine.io is a lower level library than socket.io so the socket.io is built on engine.io. http://stackoverflow.com/questions/8542502/whats-the-difference-between-engine-io-and-socket-io – Footniko Sep 17 '15 at 14:49