最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - socket.io server-side timeout for inactive client connection? - Stack Overflow

programmeradmin3浏览0评论

I know that the socket.io client library will close the current socket.io connection (and then attempt to reconnect) if it is not regularly receiving a response to the ping packets that it sends to the server (under the assumption that the connection has died for some reason). And, there are client options for controlling this reconnect behavior.

But, what happens server-side if a client goes inactive and stops sending ping messages (say because the client went to sleep)? I can't find any info in the socket.io server-side doc that explains that situation or allows for configuration of it. Will the server close an inactive client socket.io connection (one that it is not receiving ping messages from)? If so, how long will the server wait and is that behavior configurable?

I know that the socket.io client library will close the current socket.io connection (and then attempt to reconnect) if it is not regularly receiving a response to the ping packets that it sends to the server (under the assumption that the connection has died for some reason). And, there are client options for controlling this reconnect behavior.

But, what happens server-side if a client goes inactive and stops sending ping messages (say because the client went to sleep)? I can't find any info in the socket.io server-side doc that explains that situation or allows for configuration of it. Will the server close an inactive client socket.io connection (one that it is not receiving ping messages from)? If so, how long will the server wait and is that behavior configurable?

Share Improve this question edited Jun 22, 2015 at 23:15 jfriend00 asked Jun 22, 2015 at 23:07 jfriend00jfriend00 707k103 gold badges1k silver badges1k bronze badges 5
  • You can write your own code for this behavior if you can't find something suitable in the socket.io docs. – Blubberguy22 Commented Jun 23, 2015 at 15:55
  • re. server-side, would socket.on('disconnect', ...) handle any client non response after a timeout? disconnect catches the client leaving the page and you can use io.engine.clientsCount to show remaining clients. – Data Commented Aug 26, 2015 at 19:24
  • @Data - A disconnect only happens if a client shuts-down in an orderly fashion and closes their socket, not if they just lose connectivity. When a client just loses connectivity, nothing is sent to the server at that moment. The server has to somehow realize, hmmm I haven't heard anything from this client in a long time - I wonder if they're gone now. That ping/pong part of the socket.io protocol is to help detect this condition. – jfriend00 Commented Aug 26, 2015 at 19:34
  • does this help: "non-activity, io will close a socket automatically", stackoverflow.com/a/9708962/1837472 – Data Commented Aug 26, 2015 at 19:40
  • April 2024 Update: Socket.io v4 The server is now sending PING and the client sends back PONG. See Socket.io: Heartbeat Mechanism Reversal for more details. – nicholaswmin Commented Apr 3, 2024 at 11:26
Add a comment  | 

3 Answers 3

Reset to default 8

As per the socket.io 2.20 readme (the latest version as of Jan 2020):

"A heartbeat mechanism is implemented at the Engine.IO level, allowing both the server and the client to know when the other one is not responding anymore.

That functionality is achieved with timers set on both the server and the client, with timeout values (the pingInterval and pingTimeout parameters) shared during the connection handshake."

So, you can configure both the server and client side timeout settings by setting the following timeout properties on the engine.io component inside of socket.io.

Using socket.io version 2.20:

const io = ...; // initialize socket.io how you wish
io.eio.pingTimeout = 120000; // 2 minutes
io.eio.pingInterval = 5000;  // 5 seconds

The same thing using older versions of socket.io:

const io = ...; // initialize socket.io how you wish
io.set('heartbeat timeout', 1200000);
io.set('heartbeat interval', 5000);

Disconnection detection

The Engine.IO connection is considered as closed when:

  • one HTTP request (either GET or POST) fails (for example, when the server is shutdown)
  • the WebSocket connection is closed (for example, when the user closes the tab in its browser)
  • socket.disconnect() is called on the server-side or on the client-side There is also a heartbeat mechanism which checks that the connection between the server and the client is still up and running:

At a given interval (the pingInterval value sent in the handshake) the server sends a PING packet and the client has a few seconds (the pingTimeout value) to send a PONG packet back. If the server does not receive a PONG packet back, it will consider that the connection is closed. Conversely, if the client does not receive a PING packet within pingInterval + pingTimeout, it will consider that the connection is closed.

The disconnection reasons are listed here (server-side) and here (client-side).

pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000)

pingInterval (Number): how many ms before sending a new ping packet (25000)

https://socket.io/docs/v4/server-api/

https://github.com/socketio/engine.io#methods-1

On socket.io 2.20. This can be set when, initializing socket.io

const io = require("socket.io")({pingTimeout: 10000, pingInterval: 30000});

See all options and their defaults here

发布评论

评论列表(0)

  1. 暂无评论