Socket.io with flask-socketio python. How to set socket keepalive/timeout

You can also set parameters in the server-side with Flask-SocketIO:

socketio = SocketIO(ping_timeout=10, ping_interval=5)

:param ping_timeout: The time in seconds that the client waits for the
                     server to respond before disconnecting. The default is
                     60 seconds.
:param ping_interval: The interval in seconds at which the client pings
                      the server. The default is 25 seconds.

The big picture issue is that if your server is non-responsive to keep-alive packets for some extended period of time, the client will drop the connection and try to reconnect. If it cannot reconnect, eventually, it will stop trying.

That said, if you want to modify the configuration of the retry logic, then you can send an options object as the 2nd argument to your .connect() call. Per the documentation here, there is control over the following options:

Options:

  • reconnection whether to reconnect automatically (true)
  • reconnectionDelay how long to wait before attempting a new reconnection (1000)
  • reconnectionDelayMax maximum amount of time to wait between reconnections (5000). Each attempt increases the reconnection by the amount specified by reconnectionDelay.
  • timeout connection timeout before a connect_error and connect_timeout events are emitted (20000)

So, if you want it to keep trying to reconnect automatically for a much longer period of time, you can increase the times for the last three options.