WebSocket SDK
The @raptortrade/solana SDK is the recommended way to use the WebSocket transport: typed market classes query and stream fully typed data over a single connection. It is built on the low-level @raptortrade/client, documented at the end.
Install
npm install @raptortrade/solana
# or: pnpm add @raptortrade/solanaConnect
Use connect_ws to open the query and streaming sockets, then build market helpers on top of the client. See WebSocket Authentication.
import { connect_ws } from "@raptortrade/solana";
// Connect to the WebSocket transport
const client = await connect_ws({
rpc_url: "wss://rpc.raptor.trade",
sub_url: "wss://sub.raptor.trade",
token: process.env.RAPTOR_TOKEN,
});Query
Create a market with the topics you need, then call get(options) for fully typed data. Windowed topics take duration - the SDK maps it to the wire window param.
import { connect_ws, Market } from "@raptortrade/solana";
const client = await connect_ws({
rpc_url: "wss://rpc.raptor.trade",
sub_url: "wss://sub.raptor.trade",
token: process.env.RAPTOR_TOKEN,
});
const market = Market.create(client, ["ohlcv"]);
// Typed query over WebSocket - returns Ohlcv[]
const candles = await market.ohlcv.get({
base: "DezXAZ8z7PnrnRJjz3wXBoRgixCa6xjnB7YaB1pPB263",
duration: "1m",
limit: 100,
});Subscribe
Call subscribe(options, callbacks) on a topic to open a real-time stream. The server sends a hydration snapshot first, then live changes; on_insert, on_update, and on_delete receive typed data, matched by id. Call sub.unsubscribe() to stop a stream.
const market = Market.create(client, ["trades"]);
// Live stream - a hydration snapshot first, then changes
const sub = await market.trades.subscribe(
{ base: "Dez...", quote: "So111..." },
{
on_insert: (rows) => { /* hydrated + new rows */ },
on_update: (rows) => { /* revised rows, match by id */ },
on_delete: (rows) => { /* removed rows */ },
},
);
// stop this stream
await sub.unsubscribe();
// close the socket and all of its streams
client.disconnect?.();Markets and topics
- DEX market -
Market. Exposestrades,ohlcv,block,twap,vwap,volume,token. - PriorityFee -
block,fee,stats,cluster,recommendation,top_txs,top_accounts,account_block,account_stats,account_recommendation.
Each topic decodes into its typed data (Ohlcv, Trade, Twap, Vwap, Volume, Block, Token); priority-fee topics decode into their own data types. Every data point carries a numeric id.
Low level
@raptortrade/solana is built on the low-level @raptortrade/client transport (and re-exports its connect_ws). To write the method and params yourself, call the client directly with the wire window param. query_batch sends up to 64 queries over the same socket, and subscribe takes the same callbacks as the typed helpers.
import { connect_ws } from "@raptortrade/client";
// @raptortrade/client is the transport @raptortrade/solana wraps
const ws = await connect_ws({
rpc_url: "wss://rpc.raptor.trade",
sub_url: "wss://sub.raptor.trade",
token: process.env.RAPTOR_TOKEN,
});
// method + params over the wire (note: window, not duration)
const rows = await ws.query("solana::market::ohlcv", { base: "Dez...", window: "1m" });
// batch up to 64 queries over the same socket
const results = await ws.query_batch([
{ method: "solana::market::ohlcv", params: { base: "Dez...", window: "1m" } },
{ method: "solana::market::token", params: { mint: "Dez..." } },
]);
// raw subscribe
const sub = await ws.subscribe(
"solana::market::trades",
{ base: "Dez...", hydrate: 50, throttle: "1s" },
{
on_insert: (rows) => console.log("inserts", rows),
on_update: (rows) => console.log("updates", rows),
on_delete: (rows) => console.log("deletes", rows),
},
);
await sub.unsubscribe();
ws.disconnect?.();