Replace Solana Price Polling with a WebSocket Feed
Stream live Solana token prices over WebSocket with Datastream instead of polling REST endpoints on an interval.
Price polling is always late
REST polling for prices is wasteful: you keep asking even when nothing changed, and you're always one interval behind. A WebSocket pushes updates the moment a trade happens. A Datastream price subscription pushes the quote when the market moves, which is the behavior price tickers and bots actually need.
What you need
- Node.js 18+
- A Solana Tracker API key (free) and Datastream key (Premium+, same dashboard), WebSocket Datastream starts on Premium
Step 1: Connect
import WebSocket from 'ws';
(globalThis as any).WebSocket = WebSocket;
const { Datastream } = await import('@solana-tracker/data-api');
const ds = new Datastream({ wsUrl: `wss://datastream.solanatracker.io/${process.env.ST_DATASTREAM_KEY}` });
ds.on('connected', () => console.log('Live'));
ds.connect();
Step 2: Subscribe to a token's price
ds.subscribe.price.aggregated(MINT_ADDRESS).on((update) => {
console.log(update.token, update.aggregated.median, update.aggregated.median);
});
Step 3: Track multiple tokens
const priceState = new Map<string, number>();
const mints = [MINT_A, MINT_B, MINT_C];
for (const mint of mints) {
ds.subscribe.price.aggregated(mint).on(u => priceState.set(u.token, u.aggregated.median));
}
Step 4: Reconnect when the feed drops
ds.on('disconnected', () => {
setTimeout(() => ds.connect(), 1000);
});
Tip: combine the WebSocket feed with a one-off REST call (
/tokens/{mint}) at startup, so your dashboard shows an initial value immediately and then moves live.
FAQ
Why WebSocket instead of REST?
You get updates push-based within milliseconds, without the rate-limit waste of constant polling.
How many tokens can I subscribe to at once?
Multiple; the Datastream supports many concurrent subscriptions with unlimited messages on the right tier.
What if the connection drops?
Catch the disconnected event and reconnect with a short backoff, as in step 4.
Related: Get a Solana token price (REST) -> · Solana Data API
Download the example
Clone the full runnable project on GitHub, npm install && npm start with your keys in .env. Or open it in StackBlitz to run in your browser (free). See the tutorial for step-by-step context.
Runnable Node.js project — clone from GitHub, add keys to .env, then npm start. StackBlitz runs REST and Datastream examples in your browser for free.