---
source: https://iinventedlight.com/library/notes/websockets-keepalive-and-reconnect.md
title: "WebSocket keepalive, ping/pong and reconnecting clients"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (python-websockets/websockets (GitHub)); BSD-3-Clause (python-websockets/websockets (GitHub)); MIT (websockets/ws (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: coding-references
topic_area: "code-web: protocols"
tags: [websocket, keepalive, ping, pong, reconnect, python, node]
client: Librarian
sources:
  - id: S1
    title: "Keepalive and latency (websockets docs)"
    publisher: python-websockets/websockets (GitHub)
    url: https://github.com/python-websockets/websockets/blob/main/docs/topics/keepalive.rst
    accessed: 2026-11-16
    license: "BSD-3-Clause"
  - id: S2
    title: "Client FAQ (websockets docs)"
    publisher: python-websockets/websockets (GitHub)
    url: https://github.com/python-websockets/websockets/blob/main/docs/faq/client.rst
    accessed: 2026-11-16
    license: "BSD-3-Clause"
  - id: S3
    title: "ws README"
    publisher: websockets/ws (GitHub)
    url: https://github.com/websockets/ws/blob/master/README.md
    accessed: 2026-11-16
    license: "MIT"
---
# 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_interval` and `ping_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 `connect` as an asynchronous iterator: `async for websocket in connect(...)`, catching `ConnectionClosed` and calling `continue`. 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

- [server-sent-events-format](https://iinventedlight.com/library/notes/server-sent-events-format/)
- [python-http-timeouts-and-retries](https://iinventedlight.com/library/notes/python-http-timeouts-and-retries/)

## 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:`connect` as 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
