Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 606
jfriend00 Avatar asked Jun 22 '15 23:06

jfriend00


People also ask

Does Socket.IO auto reconnect?

In the first case, the Socket will automatically try to reconnect, after a given delay.

Does Socket.IO reconnect after disconnect?

Socket disconnects automatically, reconnects, and disconnects again and form a loop. #918.

How do you check if a Socket is connected or not online?

var socket = io. connect(); console. log('check 1', socket. connected); socket.


2 Answers

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);
like image 163
Pi Da Avatar answered Sep 22 '22 10:09

Pi Da


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

like image 33
Vishnu Avatar answered Sep 22 '22 10:09

Vishnu