Heartbeats and Zombie Connections

By John Sullivan · November 29, 2026 · 8 min read · #websocket #heartbeat #keepalive #zombie connection #json-rpc

Your WebSocket consumer closed for no visible reason, or worse, stayed open and delivered nothing. A keepalive — a ping/pong frame or an application-level message — fixes the first problem and not the second: it keeps a quiet connection from being closed, and it does not prove the other end is processing. Those are two different failures, and they need two different signals.

The idle timer measures what the client sends, not what the server pushes, so a consumer can be dropped for silence while events are still arriving [1] — the subscription lifecycle covers why the timer exists and what the close looks like. Three things decide whether your keepalive works: which liveness signal you send, whether it can detect a zombie, and how the interval fits inside the timeout you already have.

Two kinds of liveness: WebSocket ping/pong and an RPC keepalive

The WebSocket protocol has a ping. RFC 6455 describes control frames as "not intended to carry data for the application but instead for protocol-level signaling" [2], and the IANA WebSocket Opcode Registry lists Ping and Pong as assigned entries [3]. An application-level heartbeat is a different tool: a message your application sends on a schedule to prove it is still there. What is absent on this service is the second, not the first.

A protocol-level ping proves the socket and the transport are alive. An application-level message proves the application on the other end is reading. A connection can pass the first and fail the second. TCP keepalive sits below both, and the kernel sends it only when the socket option is enabled [4].

The docs say "Any message (including a ping) resets the timer" [5]. They do not say whether that ping is a protocol-level control frame or an application-level message, and we will not resolve it here.

Layer What it is What a success proves What it cannot detect Source
TCP keepalive A probe sent by the kernel when the socket option is enabled The peer's TCP stack is reachable Anything above the transport; off unless enabled [4]
WebSocket control frame (Ping/Pong) A control frame the RFC describes as protocol-level signaling The socket and transport are alive Whether the peer's application is reading [2] [3]
Application-level heartbeat A JSON-RPC request with an id, answered within a deadline The application on the other end is reading and responding What was missed while it was not [6]

The zombie connection: open, and not working

A stalled event loop, a blocked write, a peer that stopped reading. In each case the socket stays open and the transport stays healthy, and nothing is being processed at the other end. A protocol-level ping succeeds throughout, because the transport genuinely is healthy. The consumer looks connected and is not consuming.

The only reliable signal is an application-level round trip. Send something the peer must answer, and require the answer within a bound. A fire-and-forget heartbeat proves nothing, because a write to a socket succeeds whether or not anyone reads it: "No indication of failure to deliver is implicit in a send()" [7]. A successful write means the bytes were handed to the local stack, not that the peer's application read them.

The shape of the message matters as much as the schedule. A JSON-RPC notification has no id, and "The Server MUST NOT reply to a Notification" [6]. A notification-shaped heartbeat is structurally incapable of carrying a response deadline. Subscription pushes have the same problem from the other direction: they arrive as eth_subscription notifications carrying a subscription id and a result and nothing else [8], so the stream cannot tell you the peer is alive.

A heartbeat without a response deadline is a liveness check that cannot fail, which makes it decoration. The deadline is the part that does the work. Once a zombie is detected and a gap exists, gap detection after a drop covers the pattern: name the gap and you can backfill it; fail to name it and you are re-indexing [9].

Sizing the interval against a known timeout

The docs put the idle timeout at 30 minutes of inactivity [5]. That figure is your input. The interval is your output, and there is no documented interval to copy.

The rule: the interval plus the response deadline has to fit inside the timeout, with margin for one missed beat. The margin covers a beat that never goes out — a skipped send, not a lost answer. If one send is skipped, the next one still has to be sent and answered before the timer expires, so the version with margin is 2 × interval + deadline ≤ timeout. A lost answer is a different failure: the deadline fires at interval + deadline and the socket is closed on purpose. Substitute your own timeout and solve for the interval.

The trade runs in both directions. A longer interval costs fewer messages and leaves less room for a missed beat; a shorter one buys margin and spends messages. A heartbeat is a message, and the endpoint is metered by messages per second [5], so the interval is a trade between safety margin and message volume. The right answer depends on the timeout rather than on a preference.

The keepalive loop

A heartbeat is a request with an id, and the deadline is armed when it is sent.

// Interval and deadline are yours to derive: 2 × interval + deadline ≤ timeout.
const HEARTBEAT_INTERVAL_MS = Number(process.env.HEARTBEAT_INTERVAL_MS);
const RESPONSE_DEADLINE_MS = Number(process.env.RESPONSE_DEADLINE_MS);
const socket = new WebSocket(process.env.WS_URL!);
let nextId = 1, pendingId: number | undefined, deadlineTimer: ReturnType<typeof setTimeout> | undefined;

function sendHeartbeat() {
  // A deadline still pending here means the last answer never arrived: leave it armed.
  if (pendingId !== undefined) return;         // one beat outstanding at a time — a pending deadline IS the detection
  pendingId = nextId++;                        // a request with an id, never a notification
  socket.send(JSON.stringify({
    jsonrpc: "2.0", id: pendingId,
    method: "eth_getBlockByNumber", params: ["latest", false],
  }));
  deadlineTimer = setTimeout(() => {           // armed on send, not on connect
    pendingId = undefined;                     // a late answer is ignored, not treated as recovery
    socket.close();
    // reconnect with backoff: see the 429/backoff piece. Do not invent a schedule here.
  }, RESPONSE_DEADLINE_MS);
}
const beatTimer = setInterval(sendHeartbeat, HEARTBEAT_INTERVAL_MS);
socket.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  if (msg.id !== undefined && msg.id === pendingId) { clearTimeout(deadlineTimer); pendingId = undefined; }
};
socket.onclose = () => { clearInterval(beatTimer); clearTimeout(deadlineTimer); pendingId = undefined; };

Three details carry the correctness. The deadline starts on send rather than on connect, so a connection that opens and then stalls is caught. A late answer that arrives after the deadline has fired is ignored rather than treated as recovery. The timer is cleared on close, so a reconnect does not inherit a stale deadline — and it is not cleared before a send, because a deadline still pending when the next beat is due is the signal that the previous answer never arrived: the send is skipped while one is outstanding. Clearing it there would cancel the only thing that can close a dead connection.

The heartbeat sends eth_getBlockByNumber as a request, so the answer comes back correlated by id [6]. The reconnect schedule is not here: 429, backoff and jitter covers it, and a schedule invented in this block would be a number with nothing behind it. A heartbeat that times out and a request that returns an error are different failures, and which JSON-RPC errors are retryable covers the second.

When a heartbeat is the wrong tool

A heartbeat detects a dead connection. It does not recover what was missed while the connection was dead: "The stream cannot tell you what it did not deliver" [9], and detecting and filling the gap after a drop is the piece that does. A heartbeat also does not make a slow consumer fast. If the peer is processing slowly, a heartbeat adds load to a system already behind, and the extra messages compete with the work you are trying to protect.

A heartbeat does not replace a cursor either. The two are complementary: a consumer with a heartbeat and no cursor can tell you the connection died and cannot tell you what it missed. A notification carries no response object, so nothing about it can be confirmed [6] — the stream keeps no record of its own gaps.

A keepalive is a liveness mechanism, not a correctness mechanism. Treating it as the second is how a consumer ends up looking healthy and being wrong.

What this does not cover

Why the timer exists, what the close message says and what the reconnect loop looks like are the subscription lifecycle piece's subject. What to do once a gap exists — the cursor, the sizing, the fill and the reconciliation — belongs to the gap-detection piece. And these behaviours are this service's: another provider may send its own heartbeat or document an interval, and nothing here claims otherwise.


Three checks decide whether the keepalive you have is doing anything. Run them against the consumer in front of you rather than against the design you meant to build.

  1. Does the heartbeat carry a response deadline, or is it fire-and-forget?
  2. Does the interval plus the deadline fit inside the idle timeout with margin for one missed beat?
  3. Is there a cursor for what was missed while the connection was down?

If the heartbeat is fire-and-forget, it cannot detect a zombie, and the connection can be open and dead at the same time.

Sources

  1. eth_subscribe: reconnect, gaps, and when to poll instead — SwiftNodes, 2026-11-24
  2. RFC 6455 The WebSocket Protocol — §1.2 Protocol Overview — IETF / RFC Editor, 2011-12-01
  3. WebSocket Opcode Registry (opcode.csv) — IANA, 2011-10-21
  4. tcp(7) — Linux manual page (tcp_keepalive_time) — man7.org (Linux man-pages), 2026-04-19
  5. SwiftNodes docs — WebSocket (Idle Timeout) — SwiftNodes
  6. JSON-RPC 2.0 Specification — Notification — jsonrpc.org
  7. send(2) — Linux manual page — man7.org (Linux man-pages), 2025-10-29
  8. execution-apis src/eth/subscribe.yaml — eth_subscribe — ethereum/execution-apis (GitHub)
  9. Detecting and Filling the Gap After a WebSocket Drop — SwiftNodes, 2026-11-29

Read the WebSocket guide

This article was researched, written and edited by AI under human supervision, and checked against Rankwrite's public editorial rubric.

J
John Sullivan
Infrastructure Writer, SwiftNodes

John Sullivan covers RPC infrastructure, node operations, and multi-chain development at SwiftNodes — what it actually takes to keep endpoints fast, fresh, and reliable across EVM and non-EVM networks.

Related posts

Try SwiftNodes free — multi-chain RPC across 140 networks, flat-rate pricing, pay by card or crypto, no KYC. Get an API key in 30 seconds →