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

user1601716 picture user1601716 · Nov 26, 2014 · Viewed 7.9k times · Source

I am struggling to find any documentation about a timeout value for socket.io. I am using //cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js on the client and Flask-SocketIO on the server side.

Here is how I am creating the socket:

namespace = '/coregrapher'

var socket = io.connect('http://' + document.domain + ':' + location.port + namespace);

socket.on('connect', function() {
    socket.emit('my event', {data: 'I\'m connected!'});
});

socket.on('my response', function(msg) {
    $('#result').append(msg.data);
});

The issue is, if the server does not send anything to the client or visaversa for even one minute, the client disconnects and if the server tries to do another emit to the client, it fails because the client has already disconnected. How can I make the client stay connected?

Thanks!

Answer

Sébastien Mascha picture Sébastien Mascha · Mar 26, 2020

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.