eth_subscribe: reconnect, gaps, and when to poll instead

By John Sullivan · November 24, 2026 · 8 min read · #eth_subscribe reconnect missed blocks

Your eth_subscribe stream went quiet, you reconnected, and now you cannot tell whether the reconnect missed blocks. Two facts decide it. The idle timer counts client messages rather than subscription pushes, so a consumer receiving a steady stream of events can be disconnected for being quiet. And when the socket closes, nothing on the server records what you missed — no per-client gap record, no delivery cursor, no replay. Both come from what we stated on 2026-11-03.

The idle timer counts the wrong thing

The docs put the figure at 30 minutes [1]. What the timer measures is the client's own traffic: it runs from the last message the client sent, and subscription pushes do not reset it (as we stated on 2026-11-03).

That inverts the usual reading of idle. A consumer that has received an event every few seconds for half an hour is, by this timer, silent. It gets disconnected while the data is still arriving, and its logs show a healthy stream that stopped — the shape of a network fault, not of a policy. A consumer that also issues its own requests is not the quiet case. One that only listens is.

The server does send something before it goes: {"error":"Connection closed: idle timeout (30 min)"}, then it closes the socket. Log that message verbatim. It separates an idle close from a dropped connection, and the two need different responses: one is your own quietness, the other is the network. Without the message you cannot tell them apart after the fact.

No application-level heartbeat is sent. The WebSocket protocol does define ping and pong: RFC 6455 §1.2 describes control frames as "not intended to carry data for the application but instead for protocol-level signaling" [2], and the IANA registry lists Ping and Pong among them [3]. What is absent here is an application-level heartbeat, which is a different thing from a protocol-level frame. For a consumer that only listens, the timer is the thing to design around; how to design around it belongs to the keepalive question, and to heartbeats and zombie connections.

The gap is yours

There is no per-client gap record, no delivery cursor, and no replay of subscription events (as we stated on 2026-11-03). Nothing on the server can tell you what you missed.

That is a consequence of where the state lives, not an oversight. A server that kept a cursor per consumer would hold delivery state for every client on the endpoint, forever, and would have to decide how long to keep it. Pushing that state to the client is what makes the reconnect-and-backfill shape work: you know your own position, so you can resume from it.

The docs point out that a client library such as ethers will restore the connection and the subscription on its own [4]. Restoring both is not catching up. The subscription comes back; the events between the close and the resubscribe do not, because there is no record to replay. The docs also state that subscriptions do not survive a drop, so every reconnect needs a fresh eth_subscribe call [4]. Resubscribing is one act and backfilling is another, and the docs cover the first.

The second is yours. Persist the last block or event you processed, and on reconnect backfill from last + 1 with eth_getBlockByNumber or eth_getLogs. A reconnect can also redeliver, so the handler has to be idempotent — the mechanics of that, and of proving you have no gap, are covered in gap detection after a drop. What the backfill costs in log ranges is a separate budget, worked through in planning an indexer backfill.

The notification shape is standardised [5]; the recovery contract is not. JSON-RPC 2.0 defines a notification as a request with no id member and requires the server not to reply to one [6] — a general concept, not a subscription mechanism. The eth_subscribe definition in execution-apis defines the method and the notification, and says nothing about reconnect or replay [5]. RFC 6455 defines the closing handshake [2]; it does not define subscription recovery.

Document Defines eth_subscribe? Defines notification shape? Defines reconnect or replay?
JSON-RPC 2.0 specification (2010) No No — only the general Notification concept No
execution-apis src/eth/subscribe.yaml Yes Yes — method eth_subscription, params { subscription, result } No
RFC 6455 (WebSocket) No No Defines the closing handshake and abnormal closure, not subscription recovery

So the recovery contract is implementation-defined, and on this service it is the one we described on 2026-11-03.

Why the socket closes instead of failing over

Mid-session upstream failover is deliberately not attempted. Moving a live subscription to a different upstream would either drop events or duplicate them, so the service closes the socket and lets the client reconnect to a healthy upstream.

That is a boundary, not a defect. A silent move would leave you with a subscription that is still open, still delivering, and no longer continuous — the worst state to be in, because nothing in your client would fire. A close is a signal you can act on. The reconnect loop is the correct client design for this service, not a workaround for it.

The cost is that the boundary only helps if you handle it. A client that ignores close events gets the silent version anyway, with no reconnect and no backfill. The close is the only signal you get, and the loop is built around it.

The reconnect loop

One loop carries the whole client contract: a persisted cursor, a backfill that runs before the subscription resumes, and a handler that treats a redelivered block as a no-op. It uses a plain WebSocket and the documented JSON-RPC methods, so there is no SDK to keep in step.

let cursor = await loadCursor();                 // last block fully processed
async function handle(block) {
  if (await alreadyStored(block.hash)) return;   // redelivered: no-op
  await storeBlock(block);                       // idempotent upsert
  cursor = Number(block.number);
  await saveCursor(cursor);                      // persist before ack
}
function connect() {
  const socket = new WebSocket(ENDPOINT);
  socket.onopen = async () => {
    const head = Number((await rpc("eth_getBlockByNumber", ["latest", false])).number);
    for (let n = cursor + 1; n <= head; n++) {   // backfill BEFORE resubscribing
      await handle(await rpc("eth_getBlockByNumber", [toHex(n), false]));
    }
    socket.send(JSON.stringify({ jsonrpc: "2.0", method: "eth_subscribe", params: ["newHeads"], id: 1 }));
  };
  socket.onmessage = (e) => {
    const msg = JSON.parse(e.data);
    if (msg.method === "eth_subscription") handle(msg.params.result);
  };
  socket.onclose = () => {
    // backoff: see the 429/backoff piece — do not invent a schedule
    setTimeout(connect, nextDelay());
  };
}

Three details carry the correctness. The cursor is written after the block is stored, so a crash between the two re-processes one block rather than skipping it. The backfill runs inside onopen and completes before the eth_subscribe call, so the subscription never resumes ahead of the data. And alreadyStored is what makes redelivery harmless — without it, the backfill's overlap with the live stream writes the same block twice.

The delay in onclose is the one thing left open. A fixed retry against a failing endpoint turns a blip into a load problem, and the schedule that avoids it is its own subject: 429, backoff and jitter. The backfill calls can fail too, and some failures should not be retried — which JSON-RPC errors are retryable covers the split.

When to poll instead

A subscription earns its place when you need to react to new events within a block or two, and when a gap you detect and fill afterwards is acceptable. That is the live-tail case: a dashboard, a liquidation watcher, a mempool-adjacent consumer.

Polling is the better tool in three situations. The read is a bounded backfill rather than a live tail, so there is nothing to subscribe to. The consumer cannot tolerate a gap it did not detect — an accounting job, a settlement check — and a poll that returns a range is easier to make provably complete than a stream that might have holes. Or the volume is low enough that a poll interval costs less than holding a socket open and reconnecting it.

We would not put a subscription in front of a consumer that has to be provably gap-free. The honest framing is that a subscription is not a reliability upgrade over polling — it is a latency improvement that moves the correctness burden onto the client.

What this does not cover

How to keep a quiet connection open is a separate problem with its own trade-offs. The 30-minute timer is the constraint any keepalive design has to work within; the design itself belongs to the piece on heartbeats and zombie connections.

Proving you have no gap — the detection method in depth, including how to reconcile a backfill against the live stream — belongs to the piece on detecting and filling the gap after a drop.

These behaviours are this service's. Another provider may keep a delivery cursor, or move a subscription between upstreams mid-session, and nothing here says otherwise. Test the provider you are on rather than assuming this shape.

The one thing to check in your own consumer is ordering: does the cursor advance before the block is durably stored, or after? If it advances first, a crash between the two skips the block — the cursor sits past data you never wrote, and the backfill resumes beyond it. Store the block, then advance the cursor.

Read the WebSocket guide

Sources

  1. SwiftNodes docs — WebSocket (Idle Timeout) — SwiftNodes
  2. RFC 6455 The WebSocket Protocol — §1.2 Protocol Overview (control frames are protocol-level signalling) — IETF / RFC Editor, 2011-12-01
  3. WebSocket Opcode Registry (opcode.csv) — IANA
  4. SwiftNodes docs — How to Use Ethereum WebSocket RPC (Common pitfalls) — SwiftNodes
  5. execution-apis src/eth/subscribe.yaml — eth_subscribe and the eth_subscription notification shape — ethereum/execution-apis (GitHub)
  6. JSON-RPC 2.0 Specification — Notification — JSON-RPC Working Group (jsonrpc.org)

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.

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