All Resources
Stream a Live Solana Trade Tape over WebSocket
Data APITypeScriptJune 8, 20265 min read

Stream a Live Solana Trade Tape over WebSocket

Receive token swaps in real time over WebSocket, including side, wallet, amount and pool-scoped trade data.

data-apiwebsockettradesdatastreamtypescript

A live trade tape needs swaps, not just prices

Price WebSockets tell you where a token traded; a trade stream tells you who swapped, how much, and in which direction. Production apps backfill recent trades over REST, then open ds.subscribe.tx.token(mint) on the Datastream for live tape output.

What you need

  • Node.js 18+
  • Datastream key (Premium+)
  • REST API key (recommended, token context + backfill)

Step 1: Backfill context over REST

Before going live, fetch token metadata and the last few trades so your UI is not empty:

const info = await client.getTokenInfo(MINT);
const history = await client.getTokenTrades(MINT);
const recent = history.trades?.slice(0, 10) ?? [];

Step 2: Connect the Datastream

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('disconnected', () => setTimeout(() => ds.connect(), 2000));
await ds.connect();

Step 3: Run a trade tape

Format each swap and track session stats (buy/sell count, volume):

const tape = { count: 0, buys: 0, sells: 0, volumeUsd: 0 };

ds.subscribe.tx.token(MINT).on((tx) => {
  tape.count++;
  if (tx.type === 'buy') tape.buys++;
  if (tx.type === 'sell') tape.sells++;
  tape.volumeUsd += tx.amountUsd ?? tx.volumeUsd ?? 0;
  console.log(tx.type ?? tx.side, tx.amountUsd ?? tx.volumeUsd, tx.wallet ?? tx.owner, tx.tx ?? tx.signature);
});

The GitHub example splits this into trade-tape.ts, format.ts, and index.ts.

Step 4: Pool-scoped trades (SOL pairs)

For SOL-quoted pairs, pool-level rooms avoid noise from unrelated routes:

ds.subscribe.tx.pool(MINT, POOL_ID).on((tx) => {
  console.log('Pool trade:', tx.type, tx.volumeUsd);
});

Price vs trades

| Feed | Room | Use when |

|------|------|----------|

| Price | subscribe.price.aggregated(mint) | Charts, tickers |

| Trades | subscribe.tx.token(mint) | Tape, flow analysis, alerts |

See the price WebSocket guide for aggregated quotes.

FAQ

Do I need REST polling too?

Fetch once at startup for context and backfill. The stream handles everything after that.

How many tokens can I stream?

Multiple concurrent subscriptions; watch your plan limits on the dashboard.

What if the connection drops?

Listen for disconnected and reconnect with backoff.

Related: Real-time price WebSocket -> · 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.