miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
274 stars 24 forks source link

Timeouts after 10 mins with error code 1006 #7

Closed imrankhan17 closed 2 years ago

imrankhan17 commented 3 years ago

I have a flask-sock app deployed using gunicorn and nginx. I get disconnected after 10 minutes (sometimes sooner) with the following error: Disconnected (code: 1006, reason: "")

Is there a way to configure the timeout length?

I'm running it with: gunicorn --bind 127.0.0.1:5655 --workers 4 --threads 20 wsgi:app

miguelgrinberg commented 3 years ago

This could be the browser closing the socket due to inactivity. The WebSocket protocol has a ping/ping mechanism, but most browsers do not implement it. The server implements the pong message right now, but it does not send pings to clients (though I've been considering adding this).

What most people do is just send a dummy message every 50 seconds or so to keep them from timing out (I believe some browsers timeout sockets at one minute).

imrankhan17 commented 3 years ago

Thanks for the suggestion. As it happens I managed to fix the issue with this: https://stackoverflow.com/questions/19304157/getting-the-reason-why-websockets-closed-with-close-code-1006/57706152#57706152

miguelgrinberg commented 3 years ago

@imrankhan17 I'm not sure that's the best solution, because making the nginx timeouts longer will (I think) affect your non-websocket routes as well. As I said above, the best to beat timeouts is to have a little interval function in the browser that sends a dummy package regularly.

imrankhan17 commented 3 years ago

I have a separate nginx config for this websocket route so that shouldn't be an issue. With your suggestion, do you mean it's the client's responsibility to send dummy messages or can I do the same from the server too?

miguelgrinberg commented 3 years ago

It does not matter who sends the packet, the only purpose is to keep some traffic in the line to prevent the timeouts. I suggested you do it from the client because it is easier. I was considering adding pings in the server as a definitive solution to this problem.

imrankhan17 commented 3 years ago

Ok great, I'll look into adding in the pings but it'd be great if you added it into the package too.

miguelgrinberg commented 3 years ago

Yes, keeping this issue open so that I don't forget!

sanhardik commented 2 years ago

@miguelgrinberg Is this something that you are working on ? Would this be a change in simple-websockets code base ?

I was thinking adding an option in initialization for enabling the ping and then checking on time in this code to send a ping.

miguelgrinberg commented 2 years ago

@sanhardik I haven't had time to look at this yet, but yes, doing this in the thread may be the best solution.