6

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!

Morgoth
  • 4,935
  • 8
  • 40
  • 66
user1601716
  • 1,893
  • 4
  • 24
  • 53
  • `socket.io` has auto-reconnect logic based on regular keep-alive packets. If the server stops responding to the keep-alives, the connection will be dropped and the client will try to reconnect. There are a bunch of parameters that control the reconnect logic to determine how long and how aggressively it tries to reconnect, but basically if your server is down for an extended period of time, the socket is going to be down. The real issue here is to figure out why your server is not responding as it should always be responsive for a `socket.io` connection to be reliable. – jfriend00 Nov 26 '14 at 22:10
  • One other thing to check is to make sure that both ends of your connection are compatible with one another since I think socket.io change the protocol when going from v0.9 to v1.0 so if you're using 0.9, you have to make sure your Flask-SocketIO is using that version of the protocol (or adjusting automatically). – jfriend00 Nov 26 '14 at 22:18
  • It is intended to have lag with connections as I am going through a programming on the server side and sending emit messages to log output in a browser. But some processes take longer than others and I need to make sure it does not disconnect. How can this be done? – user1601716 Nov 26 '14 at 22:27
  • Are ;you saying that you're dealing with a server that may be busy for a long time and therefore can't respond in a timely manner to keep-alive packets? – jfriend00 Nov 26 '14 at 22:28
  • So specifically, I am debugging, and set a breakpoint while I work on it, then when I want to continue the code, the socket has disconnected, Is there a way I can prevent this? – user1601716 Nov 26 '14 at 22:36
  • That's a bit of a specific case that only happens during debugging. I don't know how to prevent that. You've essentially taken your server temporarily offline (it is not responding to requests) when it's sitting at the breakpoint. If it is only at the breakpoint for a short period of time, the auto-reconnect logic in `socket.io` should re-establish the connection. – jfriend00 Nov 26 '14 at 22:56
  • You can look at the client socket options [here](http://socket.io/docs/client-api/#manager(url:string,-opts:object)) which let you control some of the reconnect timing. – jfriend00 Nov 26 '14 at 22:58

2 Answers2

3

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.
2

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.

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979