library / How to use Solana / WebSocket / HS0120
Parallel Kit subscriptions
One TypeScript file opens a dozen Kit slot subscriptions on Carbium at once, gives each its own AbortController, and counts Node's listener warnings while they run. It prints zero, and the text shows which shared signal triggers the warning.
What the warning says
The full message reads MaxListenersExceededWarning: Possible EventTarget memory leak detected. 11 abort listeners added to [AbortSignal]. It comes from Node and has nothing to do with Solana. It shows up in code written with @solana/web3.js 2.x and its successor @solana/kit once enough subscriptions share one abort signal to put more than ten abort listeners on it.
Every Kit subscription takes an abortSignal in subscribe({ abortSignal }). Aborting that signal is the way to end the subscription, so Kit listens for its abort event for as long as the stream runs. Node warns when an event target holds more than ten listeners for one event type, on the assumption that something forgot to remove them. One controller passed to every call inside a Promise.all is the usual way to reach eleven. The code is correct and the warning prints anyway.
One signal per stream
Two fixes silence the message. Raising the ceiling with setMaxListeners(100, signal) from node:events is a single line and it suits a program that really wants one switch to end everything. Its cost is that every stream stops together. When one stream fails, you cannot restart that stream alone without minting a fresh signal for the whole group.
Giving each subscription its own AbortController removes the shared target, so no signal ever collects more than a couple of listeners. A stream can then be ended, replaced or retried without touching its neighbours, and a parent loop that aborts every controller still gives you the single switch.
| Approach | What you get | Where it stops |
|---|---|---|
| One controller per subscription | Each stream ends and restarts alone. The warning never fires, however many streams run. | You keep a list of controllers and abort them in a loop. |
| One shared controller plus setMaxListeners | One switch for the whole group, one line of code. | The number you set is the new warning point, and a failed stream cannot be restarted alone. |
| One shared controller, no change | Works. | Prints the warning once the signal holds more than ten listeners, which a dozen subscriptions always reach. |
The file below takes the first row. It also settles every subscription on its own, so one refused or failed stream shows up as a line of output and the rest keep running.
The code
Install @solana/kit 8.6.0, export CARBIUM_KEY and run the file with Node 24, which strips the types on load. The optional argument is how many subscriptions to open, and it defaults to 12, enough to make a shared signal warn.
// HS0120 · Parallel Kit subscriptions// https://carbium.sh/library/howsolana/parallel-kit-subscriptions// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createSolanaRpcSubscriptions } 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 url = new URL(process.env.CARBIUM_WS_URL ?? "wss://services.carbium.sh");url.searchParams.set("api-key", key);const count = Number(process.argv[2] ?? 12); // 12 on one shared signal is enough to warnconst rpcSubscriptions = createSolanaRpcSubscriptions(url.toString());let warnings = 0;process.on("warning", (w) => { if (w.name !== "MaxListenersExceededWarning") return; warnings++; console.error(`${w.name}: ${w.message}`);});// One controller per subscription: each stream can be ended or restarted alone.const controllers = Array.from({ length: count }, () => new AbortController());async function watch(abortSignal: AbortSignal) { let ticks = 0; let last: bigint | undefined; try { const slots = await rpcSubscriptions.slotNotifications().subscribe({ abortSignal }); for await (const { slot } of slots) { ticks++; last = slot; } } catch (err) { if (!abortSignal.aborted) throw err; // an abort is the normal way out } return { ticks, last };}setTimeout(() => controllers.forEach((c) => c.abort()), 5000);const results = await Promise.allSettled(controllers.map((c) => watch(c.signal)));results.forEach((r, i) => { if (r.status === "fulfilled") { console.log(`subscription ${i}: ${r.value.ticks} slots, last ${r.value.last}`); } else { const why = r.reason instanceof Error ? r.reason.message : String(r.reason); console.error(`subscription ${i} failed: ${why}`); }});await new Promise((resolve) => setImmediate(resolve)); // warnings arrive on a later tickconsole.log(`MaxListenersExceededWarning count: ${warnings}`);process.exit(0);
run: node main.ts [subscriptions]
slotNotifications() returns a subscription builder, and subscribe resolves to an async iterable once the server accepts the request. Each watch call owns one controller's signal. Five seconds in, the timer aborts every controller, the for await loops end, and the abort is caught as the normal way out. The catch rethrows anything that happened while the signal was still live, so a real failure reaches Promise.allSettled as a rejected result.
The last lines wait one turn of the event loop before reading the counter, because Node emits warnings on a later tick. The handler filters on the warning's name, so other process warnings do not count.
Reading the output
A healthy run prints one line per subscription and a final count. A slot is about 200 ms, so each stream sees roughly twenty to twenty-five ticks in five seconds, and the last slot is nearly the same across all twelve.
- subscription 0: 23 slots, last 472783440 means the stream opened, delivered, and ended on abort.
- subscription 7 failed: ... means that stream was refused or its socket broke. In this file all twelve ride one server subscription, so a refusal fails every line together; with different filters the others still print.
- MaxListenersExceededWarning count: 0 means no signal passed ten listeners.
To see the warning, change controllers to hold one controller and map the same one across all twelve calls, then run node main.ts 12 again. A warning line appears on stderr once, and the final count reads 1. Node warns once per target and event type, so a later subscription does not repeat it.
On Carbium
The WebSocket endpoint is wss://services.carbium.sh, and the file adds the key as ?api-key= on the URL, the form a WebSocket accepts. Kit's createSolanaRpcSubscriptions takes that URL as a string and opens its own sockets, so the code needs no other client.
Opening a slot subscription costs 5 credits, the lightest class, and after that each MiB sent costs 20 credits. Kit merges identical subscriptions made through one client into a single server subscription, so the twelve slot streams in this file open one and cost 5 credits. Twelve different filters would each pay their own opening price. Slot ticks are small. A subscription on logs by mention costs more to open, at 25 credits, and a program or all-logs stream costs 200. A subscription the server refuses costs nothing.
Your plan caps sockets and subscriptions per account, and Free carries 5 sockets. Limits belong to the account, so every key on it draws on the same budget. Kit shares one socket among many subscriptions, which means a dozen streams with different filters count against the subscription cap and not the socket cap. A cap hit shows up as a failed line for one stream, and Promise.allSettled keeps the others alive.
Drops are a separate job. Kit does not subscribe again after a socket dies, and the loop in each watch simply ends or throws. For a silence watchdog and a jittered retry, see WebSocket keepalive and reconnect. For a worker that fans many streams into one queue, see BullMQ subscription fan-in.
Where it ends
The file shows that per-stream controllers keep Node quiet and that parallel subscriptions work on Carbium. It does not show how many subscriptions your plan allows, and it cannot: twelve identical slot streams reach the server as one subscription, so the file never comes near the cap.
It also holds no retry. If a socket drops mid-run, the affected streams end with an error and stay ended. Production code wraps each watch in a loop with a fresh controller per attempt, which per-stream controllers make easy and a shared one does not.
The same warning appears for other event targets. A shared AbortSignal passed to fetch calls, or to any API that listens for its abort event, will warn past ten in the same way. The fix above applies to those too, but the counter in the file watches the warning alone and cannot find the cause.
Finally, ten is Node's default and not a rule of Kit. If you pick the setMaxListeners route, set the number to the count of streams you plan to run, so the warning still fires when something starts adding listeners it should not.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| The warning still prints after each subscription got its own controller. | Something else still shares a signal. Search for a signal built once at module level and passed into helper functions, including ones that wrap fetch. Create it inside the function that owns the stream. |
| Some subscriptions print failed while others deliver, and the failures start at the same count every run. | The account has reached its cap on sockets or subscriptions, and a refused subscription costs nothing. Close streams you no longer need, or open fewer at a time. The cap is shared by every key on the account. |
| Every subscription fails at once with an error that carries no status. | An unknown key gets 401 at the handshake, and the socket error has no status to read. Check that CARBIUM_KEY holds the whole key. |
| Your own program never exits after its streams are done. | A controller was not aborted, so one stream still holds its socket open. Abort every controller you created, including ones from failed attempts. |
| setMaxListeners is set but the warning returns after the number of streams grows. | The ceiling is fixed at the number you chose. Raise it with the stream count, or switch to one controller per stream so it stops depending on the count. |
Questions
Is the warning a real memory leak?
No. It is a heuristic that counts listeners on one event target. A subscription that you abort removes its own listener, so a stable count past ten means many live streams share one signal and nothing is accumulating.
Can I turn the warning off with a flag?
Node has no flag that silences it for one signal. Raise that signal's ceiling with setMaxListeners(n, signal) from node:events, or use one controller per stream so no ceiling is reached.
Does one controller per subscription open more sockets?
No. The signal only ends a stream, and it has no effect on how Kit places streams on sockets. The number of sockets is the same with one controller or twelve.
Does the same issue appear with `@solana/web3.js` 1.x?
The 1.x Connection class manages its own listeners and takes no abort signal per subscription. The warning belongs to the 2.x and Kit style of passing a signal into each subscribe call.