Li Haoyi
Li Haoyi

Reputation: 15812

How to decide the Ping interval for websocket connection

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

Upvotes: 33

Views: 25177

Answers (3)

morgwai
morgwai

Reputation: 2823

in my practice something around 55seconds usually ensures reliable connections, as most intermediate proxies and NAT routers have their timeout set to at least 60s.
Note however, that a given server may set much lower idle timeout on it's side depending on a given app's specifics.

Upvotes: 1

Cade W.
Cade W.

Reputation: 389

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

Upvotes: 21

HartleySan
HartleySan

Reputation: 7830

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.

Upvotes: 8

Related Questions