Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 520
Paramore Avatar asked Jan 24 '26 00:01

Paramore


1 Answers

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

like image 114
Footniko Avatar answered Jan 25 '26 19:01

Footniko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!