Websocket
The TurbineWSClient provides async WebSocket streaming for real-time orderbook, trade, and quick market updates.
Connection
Create a TurbineWSClient and connect using the async context manager:
import asyncio
from turbine_client.ws import TurbineWSClient
async def main():
ws = TurbineWSClient(host="https://api.turbinefi.com")
async with ws.connect() as stream:
await stream.subscribe("0xMarketId...")
async for msg in stream:
print(f"Type: {msg.type}, Market: {msg.market_id}")
asyncio.run(main())The WebSocket endpoint is wss://<host>/api/v1/stream. HTTP/HTTPS URLs are converted to WS/WSS automatically.
Constructor Parameters
host
str
Yes
API host URL (HTTP or WebSocket scheme)
reconnect
bool
No
Auto-reconnect on disconnect (default: True)
reconnect_delay
float
No
Initial reconnect delay in seconds (default: 1.0)
max_reconnect_delay
float
No
Max reconnect delay in seconds (default: 60.0)
Subscriptions
Subscribe to a market to receive all updates (orderbook, trades, cancellations) for that market. Subscriptions are market-level — you subscribe to a market ID, not to individual channels.
You can subscribe to multiple markets simultaneously:
Convenience Aliases
These are aliases for subscribe() and exist for backwards compatibility:
Receiving Messages
Async Iteration
The primary way to receive messages. Iterates indefinitely until the connection closes:
Single Receive
Receive one frame of messages (the server may batch multiple messages per frame):
Returns: list[WSMessage] — one or more parsed messages from a single WebSocket frame.
Message Types
All messages are instances of WSMessage or its subclasses. The type field determines the message kind.
"orderbook"
OrderBookUpdate
.orderbook → OrderBookSnapshot
Full orderbook snapshot
"trade"
TradeUpdate
.trade → Trade
Trade execution
"quick_market"
QuickMarketUpdate
.quick_market → QuickMarket
Quick market state change
"order_cancelled"
WSMessage
.data → dict
Order cancellation
Orderbook Updates
Sent whenever the orderbook changes for a subscribed market. Contains a full snapshot (not a diff).
Trade Updates
Sent when a trade is executed in a subscribed market.
Quick Market Updates
Sent when a quick market's state changes (new market, resolution, etc.).
Connection with Retry
For long-running bots, use connect_with_retry() to handle disconnections with exponential backoff:
If the connection fails, it retries with exponential backoff (1s, 2s, 4s, ..., up to 60s). The reconnect=False setting disables retries and raises WebSocketError immediately on failure.
Closing
Close the stream or client explicitly when done:
The context manager (async with ws.connect()) handles cleanup automatically.
Complete Example
A bot that monitors a BTC quick market and logs orderbook + trade activity:
Wire Format
The WebSocket server sends newline-delimited JSON. Each frame may contain one or more JSON objects separated by \n. The client parses these automatically into individual WSMessage objects.
Subscribe message format:
Unsubscribe message format:
