Subscribing
Open a bidirectional gRPC stream and write subscription requests to begin receiving live Solana data.
Install the client library
npm install @triton-one/yellowstone-grpcConnect to the endpoint
Instantiate the client with your endpoint and API key:
import Client, { CommitmentLevel } from "@triton-one/yellowstone-grpc"; const client = new Client( "grpc.openinfra.sh:10000", undefined, { "x-token": process.env.OPENINFRA_API_KEY! }); const stream = await client.subscribe(); // Handle incoming datastream.on("data", (data) => { console.log("update:", JSON.stringify(data, null, 2));}); stream.on("error", (err) => { console.error("stream error:", err); stream.end();}); stream.on("end", () => console.log("stream closed"));Write your first subscription
After calling
subscribe(), write a subscription request to choose what data to receive:// Write a subscription request to the streamawait new Promise<void>((resolve, reject) => { stream.write( { slots: { slots: {} }, // subscribe to all slot updates accounts: {}, transactions: {}, blocks: {}, blocksMeta: {}, accountsDataSlice: [], ping: undefined, commitment: CommitmentLevel.CONFIRMED, }, (err) => (err ? reject(err) : resolve()) );}); console.log("Subscribed. Waiting for slot updates…");Keep-alive pings
The server closes idle streams after 30 seconds. Send a ping periodically:
// Send a ping every 5 seconds to keep the stream alivesetInterval(() => { stream.write({ ping: { id: 1 } }, () => {});}, 5_000);
Reconnection
- Implement exponential back-off starting at 1 second, capped at 30 seconds.
- Re-send your subscription request immediately after reconnecting.
- Track the last slot you processed so you can detect gaps after reconnection.