Skip to content
Alchemy Logo

rootSubscribe

Subscribe to notifications when the Solana validator sets a new root slot.

The rootSubscribe method opens a stream that emits a notification each time the validator sets a new root slot. The notification payload is a single number: the slot of the new root. Pair it with rootUnsubscribe to stop receiving notifications.

  • None

// initiate websocket stream first
wscat -c wss://solana-mainnet.g.alchemy.com/v2/<-- ALCHEMY APP API KEY -->
 
// then call subscription
{"jsonrpc":"2.0","id":1,"method":"rootSubscribe"}

// subscribe response
{"jsonrpc":"2.0","result":0,"id":1}
 
// notification
{
  "jsonrpc": "2.0",
  "method": "rootNotification",
  "params": {
    "result": 42,
    "subscription": 0
  }
}

Use rootUnsubscribe with the subscription id returned by rootSubscribe to cancel the stream.

  • subscription_id: number - The subscription id to cancel.
{"jsonrpc":"2.0","id":1,"method":"rootUnsubscribe","params":[subscription_id]}
{"jsonrpc":"2.0","result":true,"id":1}

When using @solana/web3.js, call connection.removeRootChangeListener(subscriptionId) instead of sending the raw JSON-RPC request.

Was this page helpful?