Skip to content

WebSockets

Describes @stxapp/stx-typescript 0.12.1, the current release.

The client streams the channels documented under WebSockets: what each channel sends, its payloads and its limits are covered there. This page shows the SDK calls.

import { STX } from "@stxapp/stx-typescript";
const client = new STX();
const ws = await client.websocket().connect();
const ticker = await ws.ticker();
console.log(ticker.reply);
await ws.close();

client.websocket() uses the client’s host and key, and signs the handshake with the key. Connecting joins nothing; each join method returns a Channel. A client with no API key connects unsigned: the market data channels accept that, and the account channels refuse the join.

Each message has topic, event and payload. Take them with a callback, one at a time, or with for await:

const ids = (await client.markets({ status: "open", limit: 5 })).items.map((m) => m.market_id!);
// A callback
await ws.trades({ marketIds: ids, onMessage: (msg) => console.log(msg.event, msg.payload) });
// One message, with a timeout in milliseconds
const book = await ws.orderbook(ids);
try {
const msg = await book.next(5_000);
console.log(msg.payload.market_id); // e00651a3-10cb-4bc1-9e1f-c5ea1796c83b
} catch {
console.log("nothing in 5 s");
}
// Every message, until the channel closes
for await (const msg of book) {
console.log(msg.payload.market_id, msg.payload.bids[0]);
// e00651a3-10cb-4bc1-9e1f-c5ea1796c83b { price: "0.5800", quantity: "210.00", liquidity: "121.8000", total_quantity: "210.00", total_liquidity: "121.8000" }
}

Money fields are dollar strings on every channel, including the ones that send cents on the wire.

Account channels send your current state right after the join. waitSnapshot() returns it, keyed by event name:

const positions = await ws.positions();
const snap = await positions.waitSnapshot(10_000);
console.log(snap.all_positions.positions);
// One position, selected fields:
// [{ market_id: "e00651a3-10cb-4bc1-9e1f-c5ea1796c83b", position: "40.00", premium: "-23.2000", buy_order_liability: "34.8000" }]
SDK call Channel
ws.orderbook(marketIds) Order book, ticker and trades
ws.ticker({ sports, competitions }) Order book, ticker and trades
ws.trades({ marketIds, eventIds }) Order book, ticker and trades
ws.market(marketId) Market order book
ws.markets({ ruleFilters, messageTypes }) Markets
ws.marketStats(marketIds, { range }) Market stats
ws.marketUpdates({ watch }) Market updates
ws.orders({ marketIds, cancelOnDisconnect, pingTimeout }) Orders
ws.fills({ marketIds }) Fills
ws.positions({ marketIds }) Positions
ws.settlements({ marketIds }) Settlements
ws.balances({ accountId }) Balances
ws.account({ marketIds }) Account
ws.userInfo() User info
ws.comboPlayer(), ws.comboMaker() Combo negotiation; see Combos

Every join method also takes onMessage. To change filters after joining, call channel.selectMarketIds([...]) (and selectFilters, selectRuleFilters, selectMessageTypes); on market_updates, channel.watch([...]). ws.join(topic, payload) joins any other topic.

ws.accountView() keeps your balance, open orders, fills and positions current. It joins the balances, orders, fills and positions channels, starts from their snapshots, applies every update, and starts over from fresh snapshots after a reconnect. It resolves once all four snapshots are in.

const ws = await client.websocket().connect();
const view = await ws.accountView({
onChange: (change) => render(change.view.state()), // change.kind: "balance" | "orders" | "fills" | "positions" | "payment"
});
console.log(view.balance?.available_balance, view.openOrders.length, view.fills.length, view.positions.length);
// 746.8600 1 1 1
// No refetch needed: the new order and the balance change arrive on the socket.
await client.placeOrder(marketId, "buy", "limit", { price: "0.01", quantity: "1" });

view.state() returns plain data you can send to a browser. Options: marketIds, accountId, maxFills (default 1000), keepFlatPositions, snapshotTimeoutMs (default 15000). view.close() leaves the four channels and keeps the socket open.

The client sends the heartbeat and channel pings for you. After a drop it reconnects with backoff, signs again, and rejoins every channel with its current filters. If the exchange closes or errors one channel while the socket stays up (for example, its cancel-on-disconnect timer fired), the client rejoins that channel the same way and calls onReconnect. Each channel is joined once per connection. It cannot know what you missed while offline, so read again anything you loaded with a client method (open orders, history) in onReconnect:

import { ReconnectPolicy, STX } from "@stxapp/stx-typescript";
const client = new STX();
const ws = await client
.websocket({
reconnectPolicy: new ReconnectPolicy({ initialBackoffMs: 500, maxBackoffMs: 30_000, maxAttempts: 20 }),
onReconnect: async () => {
const open = await client.orders({ status: ["created", "requested", "accepted", "delayed", "open"] });
console.log("reconnected, open orders:", open.length); // reconnected, open orders: 1
},
})
.connect();

reconnect: false turns reconnecting off. ws.connected and ws.reconnects report the socket’s state, and ws.channelRejoins counts channels rejoined after the exchange closed them. After a reconnect explains what the exchange does and does not replay.

A refused join throws STXChannelException, with the server’s reason in err.reply. No reply within joinTimeoutMs (default 10000) throws STXTimeoutException.

runForever() resolves when you call close() or the socket gives up:

const ws = await client.websocket().connect();
await ws.fills({ onMessage: (m) => console.log("fill", m.payload) });
process.on("SIGINT", () => void ws.close());
await ws.runForever();

STX is operated by XV Exchange, LLC, a Designated Contract Market regulated by the U.S. Commodity Futures Trading Commission (CFTC). Contracts traded on STX are cleared by XV Clearing, LLC, a CFTC-registered Derivatives Clearing Organization. Trading involves substantial risk of loss and may not be appropriate for everyone. Members risk losing the cost to enter any transaction, including fees. Consider whether trading is appropriate for you in light of your experience and financial resources. Nothing on this site is investment advice or a recommendation to trade. Past performance is not indicative of future results.

© 2026 STX. All rights reserved.llms.txtllms-full.txtView as Markdown
Docs version v1.10.5