STXWebSocket is the async-native WebSocket client. One TCP connection, many Phoenix channels, reconnect + heartbeat + resubscribe handled for you.
Quick start
What you get for free
Reconnect
Connection drops trigger exponential-backoff reconnect. Your handlers keep firing on the new socket — no code change needed.
Resubscribe
Every
join() is cached. After a reconnect the SDK replays all phx_join frames before dispatching any user messages.Heartbeat
Phoenix heartbeat frames every 30 s. Servers consider a socket stale without them and drop the connection.
Clean close
async with ws: (or await ws.close()) sends phx_leave for each active subscription before closing the socket.Channels
Import fromstx.enums.Channels:
| Channel | What it pushes |
|---|---|
MARKET_INFO | Market-level price and status updates |
ACTIVE_TRADES | Public trade tape |
ACTIVE_ORDERS | Your order status changes |
ACTIVE_POSITIONS | Your position updates |
ACTIVE_SETTLEMENTS | Settlement events |
PORTFOLIO | Balance and PnL changes |
USER_INFO | Account-level updates |
Market info
Public — anyone with a valid JWT can join:Active orders
Per-user — receives your order-status transitions in real time. Replaces pollinguserOrders.
Portfolio
Per-user — balance updates and PnL tick updates.Channels that include user-scoped data (
ACTIVE_ORDERS, PORTFOLIO, etc.) rely on the JWT to identify you. The WS client reuses the same User singleton as STX / AsyncSTX — call any authenticated sync or async method once before opening the socket, or pass email/password to STXWebSocket directly.Message shape
Every handler receives aChannelMessage:
phx_reply, phx_error) — filter for them in your handler if you don’t want to see them:
Multiple subscriptions
One socket, many channels:join() gets its own handler. The SDK routes inbound frames to the right one by channel topic.
Reconnect behaviour
The reconnect loop follows the SDK’s standardRetryPolicy — exponential backoff with jitter, up to max_attempts. Override it:
- Drops the old socket.
- Backs off (up to
retry.max_attempts). - Opens a new socket to the same host.
- Replays every
phx_joinit sent before the drop. - Resumes dispatching user frames.
Custom heartbeat interval
Default is 30 s, matching the server’s idle timeout. For tests you might want faster:Close and leave
Handler exceptions
An exception in a handler won’t kill the reader loop — it’s caught, logged atWARNING, and the next frame proceeds. This means a buggy handler can silently swallow messages. Instrument your handlers if that matters to you.
Example: market-maker skeleton
Next
Async HTTP
Pair
STXWebSocket with AsyncSTX for event-driven bots.Errors & retries
Reconnect policy and how it interacts with the retry system.

