WebSockets in Chrome and Firefox Disconnecting After One Minute of Inactivity

前端 未结 4 1758
陌清茗
陌清茗 2021-02-07 04:08

I have found that WebSockets in Chrome and Firefox disconnect after exactly one minute of inactivity. Based on stuff I\'ve seen online, I was all set to blame proxies or some s

4条回答
  •  一向
    一向 (楼主)
    2021-02-07 04:24

    As much as i understood from researching this, this is caused by websocket timing out over a period of time when no data is sent. This is probably per browser.

    You could use pings to resolve this or just reconnect when you need to use the socket again.

    It makes sense to not keep sockets open when they are not used from server side as from browser side. For example, Chrome has a limit how many connections can be open, if the limit would be 64 connections and you have open 64 tabs (which is very likely for me as i always have loads of tabs open) and each tab is connected to a server, no more connections could be done (Actually similar thing happened to me once, when i ran out of available sockets in Chrome, funny).

    There is proxy_read_timeout (http://nginx.org/r/proxy_read_timeout) which as well applies to WebSocket connections. You have to bump it if your backend do not send anything for a long time. Alternatively, you may configure your backend to send websocket ping frames periodically to reset the timeout (and check if the connection is still alive).

    https://forum.nginx.org/read.php?2,236382,236383#msg-236383

    Web Sockets have an idle timeout of 60 seconds: if you do not use a heartbeat or similar via ping and pong frames then the socket assumes that the user has closed the page and closes the socket to save resources.

    https://www.codeproject.com/Questions/1205863/Websocket-is-closed-after-min

    https://github.com/tornadoweb/tornado/issues/1070

提交回复
热议问题