Skip to main content
Use WebSocket subscriptions for changes after a connection is established. Fetch an HTTP snapshot when you also need the current state; a subscription is not a history query.

Connect and subscribe

Use Node.js 20 or later. Install ws, set an RPC API key, and choose an account that changes regularly:
Save this as accounts.mjs, run node accounts.mjs, and stop with Ctrl+C. The example unsubscribes before closing when a subscription ID is available.
accounts.mjs
A successful subscribe response confirms the subscription, not an account change. No notifications arrive until the account changes at the requested commitment.

Request IDs and subscription IDs

The id you send identifies a request and its response. The server returns a separate subscription ID in result. Notifications carry that ID in params.subscription; pass it to the matching unsubscribe method on the same connection.

Choose a subscription

See the Solana WebSocket reference for standard notification semantics. Shredstream delivers transactions before execution metadata is available. Treat them as provisional and confirm their outcome through RPC.

Recover after a disconnect

Reconnect with bounded backoff and jitter, then send the subscription requests again. IDs from the old connection cannot be reused. Refetch current account state over HTTP and reconcile it with buffered notifications using context slots. Recover transaction history separately when every event matters; reconnecting does not replay missed updates. Keep a bounded processing queue and monitor its size. If the consumer falls behind, reduce the subscription scope or move expensive decoding out of the message callback. Check your plan’s connection limits.