Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decide the Ping interval for websocket connection

Tags:

I understand the Ping is to help prevent proxies from closing idle connections. Is there any guidelines w.r.t. how often to ping? Once every second? 10 seconds? Minute? Does it even matter? A preliminary google search gives me nothing, and the websocket spec only says what a ping is, not how often you should do it

like image 222
Li Haoyi Avatar asked Jan 29 '13 05:01

Li Haoyi


People also ask

How often should you ping a WebSocket?

Maintaining the connection Mews recommends that the integration sends a WebSocket Ping request to Mews at regular intervals (e.g., every 5 minutes) to monitor the connection. If the Ping request fails, the integration interface should be restarted or try to re-establish a connection.

Does WebSocket need a ping?

Using TCP keepalive may work, but only for the TCP session from your client to the next hop -- which may or may not actually be the end server. Given the above, I would recommend that yes, you should ping your connected WebSocket sessions periodically.

What is WebSocket Ping?

The Websocket protocol implements so called PING/PONG messages to keep Websockets alive, even behind proxies, firewalls and load-balancers. The server sends a PING message to the client through the Websocket, which then replies with PONG. If the client does not reply, the server closes the connection.

How much traffic can a WebSocket handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).


2 Answers

I know this is an old question, but i've also been in search for an answer. The previous answer does not make mention of the interval, so I searched through some code of some popular websocket frameworks. Not that this is official, but at least it gives a starting point: this repo makes use of 20 second intervals. I'm not sure if that 100% correct, but its better than "often", or "somewhat frequently".

like image 71
Cade W. Avatar answered Sep 19 '22 17:09

Cade W.


The accepted answer to the following SO thread seems to answer your question pretty well:
Sending websocket ping/pong frame from browser

It sounds like you can ping fairly often (using your own custom ping/pong strings), and unless there are a ton of clients connected to your WebSocket server, then the load on the system will be fairly minimal.

like image 32
HartleySan Avatar answered Sep 23 '22 17:09

HartleySan