WebSocket keepalive, ping/pong and reconnecting clients
Answer: Idle WebSocket connections get cut: HTTP/1.1 infrastructure closes idle connections after 30 to 120 seconds, so proxies may end a WebSocket early. Ping and Pong frames provide keepalive and heartbeat.
Python websockets
- It sends a Ping frame every 20 seconds and expects a Pong within 20 seconds; else it considers the connection broken and terminates it.
- Timings are configurable with
ping_intervalandping_timeout. Shorter values detect drops faster but add traffic and are more sensitive to latency. - Browsers don't enable such a keepalive by default.
- To reconnect, use
connectas an asynchronous iterator:async for websocket in connect(...), catchingConnectionClosedand callingcontinue. Uncaught exceptions break out of the loop.
Node ws
Ping messages can verify that a remote endpoint is still responsive. The ws example sets isAlive = true on pong; its interval does if (ws.isAlive === false) return ws.terminate(), then ws.isAlive = false; ws.ping().
See also
Claims
- id: C1 claim: Idle connections close after 30 to 120 seconds. source: S1 evidence: "closes idle connections after 30 to 120 seconds"
- id: C2 claim: websockets pings every 20 seconds. source: S1 evidence: "It sends a Ping frame every 20 seconds."
- id: C3 claim: Reconnect with async for over connect. source: S2 evidence: "Use :func:
connectas an asynchronous iterator"
- id: C4 claim: ws terminates clients that miss a pong. source: S3 evidence: "if (ws.isAlive === false) return ws.terminate();"
Sources
- [S1] Keepalive and latency (websockets docs). python-websockets/websockets (GitHub). https://github.com/python-websockets/websockets/blob/main/docs/topics/keepalive.rst . License: BSD-3-Clause. Accessed 2026-11-16.
- [S2] Client FAQ (websockets docs). python-websockets/websockets (GitHub). https://github.com/python-websockets/websockets/blob/main/docs/faq/client.rst . License: BSD-3-Clause. Accessed 2026-11-16.
- [S3] ws README. websockets/ws (GitHub). https://github.com/websockets/ws/blob/master/README.md . License: MIT. Accessed 2026-11-16.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: python-websockets/websockets (GitHub), Keepalive and latency (websockets docs), BSD-3-Clause (https://raw.githubusercontent.com/python-websockets/websockets/main/LICENSE). Copyright (c) Aymeric Augustin and contributors; python-websockets/websockets (GitHub), Client FAQ (websockets docs), BSD-3-Clause (https://raw.githubusercontent.com/python-websockets/websockets/main/LICENSE). Copyright (c) Aymeric Augustin and contributors; websockets/ws (GitHub), ws README, MIT (https://raw.githubusercontent.com/websockets/ws/master/LICENSE). Copyright (c) 2011 Einar Otto Stangvik; (c) 2013 Arnout Kazemier and contributors; (c) 2016 Luigi Pinca and contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: python-websockets/websockets (GitHub), Keepalive and latency (websockets docs), BSD-3-Clause (https://raw.githubusercontent.com/python-websockets/websockets/main/LICENSE). Copyright (c) Aymeric Augustin and contributors; python-websockets/websockets (GitHub), Client FAQ (websockets docs), BSD-3-Clause (https://raw.githubusercontent.com/python-websockets/websockets/main/LICENSE). Copyright (c) Aymeric Augustin and contributors; websockets/ws (GitHub), ws README, MIT (https://raw.githubusercontent.com/websockets/ws/master/LICENSE). Copyright (c) 2011 Einar Otto Stangvik; (c) 2013 Arnout Kazemier and contributors; (c) 2016 Luigi Pinca and contributors.
Cite this
iinventedlight. 2026. "WebSocket keepalive, ping/pong and reconnecting clients." iinventedlight Library, iil:note:2eee2efv, version of 2026-11-16. https://iinventedlight.com/library/notes/websockets-keepalive-and-reconnect/. License: CC-BY-4.0.
@misc{iil_note_2eee2efv,
author = {{iinventedlight}},
title = {{WebSocket keepalive, ping/pong and reconnecting clients}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/websockets-keepalive-and-reconnect/},
note = {Permanent ID iil:note:2eee2efv. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/websockets-keepalive-and-reconnect.md.