library / How to use Solana / WebSocket / HS0110
Slot subscription reader
One TypeScript file opens slotSubscribe on Carbium, prints each notification with its slot, parent and root, flags skipped slots, then compares the last slot it saw with a getSlot call at processed commitment.
What slotSubscribe means
slotSubscribe is a WebSocket method that pushes a message each time the node takes up a new slot that has a block in it. A slot is the window in which one leader may produce one block, and on mainnet it lasts about 200 ms since the Alpenglow switch. You send one JSON-RPC request and the node answers with a subscription id. From then on a slotNotification message arrives for each such slot, with no polling.
Each notification carries three numbers.
- slot: the slot the node just reached.
- parent: the slot whose block this one builds on. It is normally slot - 1, and a smaller number means the slots in between had no block.
- root: the newest slot the node treats as rooted, which means it will not be rolled back by a fork change.
The feed describes the node's view of the chain and says nothing about what a block contains. It runs at the processed level: a notification arrives when the node first sees the slot, before any vote or confirmation. A slot announced here can still end up skipped when its fork is dropped.
The code
The file needs no install. Export CARBIUM_KEY and run it with Node 24, which strips the types on load. The optional argument is how many notifications to read before it stops, and it defaults to 10.
// HS0110 · Slot subscription reader// https://carbium.sh/library/howsolana/slot-subscription-reader// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createDefaultRpcTransport, createSolanaRpcFromTransport } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const wsUrl = new URL(process.env.CARBIUM_WS_URL ?? "wss://services.carbium.sh");wsUrl.searchParams.set("api-key", key);const rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);const want = Number(process.argv[2] ?? 10);let seen = 0;let last = 0;let skipped = 0;const ws = new WebSocket(wsUrl);const timeout = setTimeout(() => { console.error("no slot notification in 25 s"); process.exit(1);}, 25_000);ws.addEventListener("open", () => { ws.send(JSON.stringify({ jsonrpc: "2.0", id: 1, method: "slotSubscribe" }));});ws.addEventListener("error", () => { console.error("socket error: check CARBIUM_KEY and the WebSocket URL"); process.exit(1);});ws.addEventListener("message", async (ev) => { const msg = JSON.parse(String(ev.data)); if (msg.error) { console.error(`subscribe refused: ${msg.error.message}`); process.exit(1); } if (msg.method !== "slotNotification" || seen >= want) return; const { slot, parent, root } = msg.params.result as { slot: number; parent: number; root: number }; const gap = slot - parent - 1; // slots before this one that produced no block skipped += gap; last = slot; console.log(`slot ${slot} parent ${parent} root ${root} head-minus-root ${slot - root} skipped-before ${gap}`); if (++seen < want) return; clearTimeout(timeout); ws.close(); try { const head = await rpc.getSlot({ commitment: "processed" }).send(); console.log(`${seen} notifications, ${skipped} skipped slots, last pushed ${last}, getSlot processed ${head}`); } catch (e) { console.error(`getSlot failed: ${e instanceof Error ? e.message : String(e)}`); } process.exit(0);});
run: node main.ts [notifications-to-read]
The WebSocket carries the subscription and the key rides in the URL as api-key. The RPC client for the closing check is built with createDefaultRpcTransport and sends the same key in the x-api-key header. Both endpoints come from environment variables with the Carbium defaults, so a test double can stand in for either.
After the last notification the file closes the socket and calls getSlot with commitment: "processed" set explicitly. @solana/kit 8.6.0 fills in confirmed when a call leaves commitment out, while the JSON-RPC server alone would use finalized. Naming the level removes the doubt, and processed is the level that matches the slot feed.
Reading the output
Every line shows the slot, its parent, its root, how far the head runs ahead of the root and the number of slots skipped before it. A run looks like a column of slot numbers that rises by one each time, with an occasional jump.
| Field or line | What it tells you | What to do with it |
|---|---|---|
| slot | The newest slot the node has reached | Use it as a clock that ticks about five times a second |
| slot minus parent minus one | How many slots before this one produced no block | Count them to measure skipped slots over a period |
| root | The newest rooted slot the node reports | Treat everything at or below it as settled on this node |
| slot minus root | How far the head runs ahead of the rooted slot | Watch it for a node that has stopped rooting |
| closing getSlot | The head as an RPC read at processed | Compare it with the last pushed slot to see that the two paths agree |
The closing comparison should differ by a slot or two at most, because the read happens a moment after the last push and the head keeps moving. A getSlot value behind the last pushed slot is also possible, since two calls to a node can land on slightly different views. Treat a difference of a slot or two either way as noise.
Since Alpenglow the root follows the head closely, where it used to trail by about 32 slots. In a healthy run slot minus root stays at a few slots, 2 to 4 in a typical read. A gap that grows into the tens means the node you read from is behind or struggling.
What to build on it
A slot feed is a light heartbeat for a Solana client. Four jobs come up again and again.
- A clock for your own code. Read the latest slot from the feed and skip the polling call that would otherwise run every few hundred milliseconds.
- A trigger. Fetch an account or a block when a slot arrives instead of on a timer, so reads line up with the chain.
- A lag measure. Compare the slot you hold with a fresh getSlot and alert when your reader falls behind. Slack slot-lag alerts wires that into a channel.
- A liveness check for a quiet subscription. A wallet that rarely transacts produces no messages for long periods, so a slot subscription beside it shows whether the socket is still alive. WebSocket keepalive and reconnect builds a watchdog on that idea.
The file's skipped count scales up the same way. Add the parent gaps over a minute, divide by the slots in that window, and you have the skip rate for its leaders.
On Carbium
The WebSocket endpoint is wss://services.carbium.sh and the file passes the key as `?api-key=` in the URL. Opening slotSubscribe falls in the lightest class, 5 credits to open, then 20 credits per MiB sent. A refused subscription costs nothing.
Each slot message is about 130 bytes of JSON. At five a second that comes to roughly 50 MiB a day, or about 1,000 credits at the per-MiB rate. Plans cap sockets and subscriptions per account, and Free carries 5 sockets. Limits belong to the account, so every key on it shares them. One socket can carry many subscriptions, so share the connection between a slot feed and anything else you watch.
The closing getSlot is a cheap cached read at 1 cell, allowed 5 calls a second on Free and 500 on Pro. An unknown key gets 401 at the handshake, and the WebSocket error event carries no status, so check the key first when the socket will not open.
Where it ends
The feed tells you the slot and its place in the chain and nothing else. It carries no block time, no transactions and no leader. To turn a slot into a timestamp, call getBlockTime, which answers only for slots that produced a block.
A slot seen here is processed. If a decision depends on the slot staying in the chain, wait for the root to pass it, or read at confirmed or finalized through RPC. The root field only describes this node's view.
The subscription starts at the current slot and replays nothing. A reader that disconnects misses the slots in between, and the parent gap on the next message does not cover that outage. Count the difference from the last slot you saw.
The file reads once and stops. A long-running reader needs reconnect logic and a silence watchdog like the one in WebSocket keepalive and reconnect.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| The socket closes at once and no slot arrives. | An unknown key gets 401 at the handshake and the error event carries no status. Check that CARBIUM_KEY holds the whole key. |
| The subscribe reply is an error and the file exits. | A refused subscription costs nothing. Read the message the file prints and check the account's socket and subscription caps, which every key on the account shares. |
| The slot numbers jump and the skipped count rises. | A slot whose leader produced no block sends no notification. Compare parent with slot to count skipped slots, and treat a jump as normal. |
| The closing getSlot is behind the last pushed slot. | The two calls reach the node at different moments and can see slightly different views. A gap of a slot or two is noise. A growing gap points at a lagging node. |
| The run prints nothing and stops after the timeout. | Check that CARBIUM_WS_URL is unset or points at a WebSocket endpoint, then check network access to port 443. |
Questions
What is the difference between slot and root in the message?
The slot is the newest one the node has reached. The root is the newest slot the node treats as settled, which a fork change will not undo. Since Alpenglow the two sit very close together.
Does slotSubscribe tell me a block was produced?
No. It tells you the node reached a slot. Use parent against slot to see whether the earlier slots had blocks, and read the block itself if you need its contents.
Can I use the slot feed instead of polling getSlot?
Yes for a running reader. Keep one getSlot call for the start and for checks, because a new subscription replays nothing and the first message can take a moment.
Why name processed in the getSlot call?
The slot feed runs at processed, so the check reads at the same level. Left out, the call would run at confirmed, the level @solana/kit 8.6.0 fills in, and could land a slot behind the feed.