TypeScript SDK — WebSocket
The SDK provides real-time market data and account updates over WebSocket. Public streams require no authentication. Authenticated streams (orders, balances) require an auth strategy and run only on the server entry point.
Public streams
Subscribe to market data without authentication:
Code
Public streams share a single underlying WebSocket connection. Opening multiple streams to different symbols reuses the same session.
Closing a stream
Code
Closing one stream does not affect others on the shared session. Call client.close() to shut down all streams and connections.
Live order book
The SDK maintains a self-healing L2 order book from WebSocket depth data:
Code
The first "update" event after subscribing (or after a "resync") carries the full book — treat it as a replacement, not an incremental diff. Subsequent updates carry only changed levels; a quantity of "0" means the level was removed.
spread() and mid() return floating-point values for display. Do not use them for exact execution decisions without decimal handling.
Code
Authenticated streams
Authenticated streams require the server entry point and an auth strategy. They use the ws package to set custom headers on the WebSocket upgrade request.
Code
Browser limitation
Browser WebSocket cannot set custom HTTP headers on the upgrade request. Authenticated WebSocket streams (which require HMAC headers) only work from the server entry point. Browser apps can use:
- Public streams (trades, depth, book tickers) — no auth needed
- REST endpoints via OAuth for authenticated operations
WebSocket order operations
Place and cancel orders over WebSocket for lower latency:
Code
Cancellation methods that affect multiple orders require explicit confirmation:
Code
Reconnection
WebSocket connections reconnect automatically on disconnection with exponential backoff:
- Public stream subscriptions are replayed after reconnect
- Authenticated streams re-authenticate with fresh credentials (nonces and tokens are regenerated)
- Mutating requests (order placement/cancellation) are never replayed — they reject with an error if the connection drops mid-request
Monitor reconnection:
Code
Stream state
Code
GeminiWebSocket vs WsSession
The SDK exposes two lower-level WebSocket classes if you need more control:
| Class | Use when |
|---|---|
GeminiWebSocket | You want the full typed stream API (trades, depth, orders) but outside of a GeminiMarkets client |
WsSession | You want raw WebSocket request/response correlation without stream abstractions |
Most applications should use client.websocket via createClient(). The lower-level classes are for advanced use cases like custom stream routing or multi-session architectures.
What's next
- WebSocket Reference — every stream, method, and wire-format field
- Order Book Reconstruction — snapshot + diff internals and gap recovery
- Error handling — WebSocket-specific errors and connection failures
- Patterns & recipes — heartbeat, liveness checks, and advanced configuration