Websocket Gateway
Overview
The gateway ws subscription allow users to listen to data streams in real-time data, delivering instant trading updates directly from the Ethereal exchange platform. To interact with the subscription gateway API, send websocket messages after connecting to wss://ws.ethereal.trade/v1/stream.
Socket.io
Ethereal uses Socket.io, a library that extends the standard WebSocket protocol. Socket.io provides ready-made client libraries in multiple languages with automatic reconnection handling and built-in ping/pong frames to maintain stable connections, saving developers from implementing these basic features themselves.
Refer to Socket.io for language specific client implementations.
Team is now internally testing native WebSockets; Socket.io will be replaced and removed. If you are using the Python SDK, the transition should be seamless aside from a future upgrade requirement.
Migration Native WebSockets will be available on testnet first for integrators to test and provide feedback. After mainnet release, both Socket.io and native WebSockets will remain available for a transition period to allow a smooth migration without disruption.
WebSockets
Ethereal also offers WebSocket support, which communicates with plain JSON payloads, resulting in lower latency, reduced overhead, and smaller message sizes. This makes it well-suited for latency-sensitive integrations and lightweight clients that don't need the built-in reconnections provided by Socket.io.
Websockets support a subset of the Socket.io feeds. If you require additional data streams, use the Socket.io gateway above which supports the full set of subscription types.
Socket.io Subscription Streams
Socket.io Subscription StreamsSubscribe
The WebSocket gateway offers the following subscription streams:
BOOK_DEPTH - Provides order book depth updates for a specific product.
BookDepthevents are emitted on a configurable fixed interval (as of writing, this is configured to be once every 200ms)previousTimestampis in milliseconds and represents the last time theBookDepthemittedtimestampalso in milliseconds and the system timestamp of when thisBookDepthwas emittedUsing both the
previousTimestampandtimestampyou can infer whether or not any events were missed during connection or during consumptionasksan array of price/quantity tuples representing asksbidsan array of price/quantity tuples representing bids
A BookDepth message of the current book (up to 100 price levels per side) is emitted back on initial connection. Every subsequent message is a price level diff with absolute quantities. A zero quantity price diff indicates that this level has been removed.
MARKET_PRICE - Delivers real-time market price updates for a specified product.
MarketPriceevents are emitted on a configurable fixed interval (currently configured to be once every second)
ORDER_FILL - Notifies when orders are filled for a specific subaccount.
OrderFillevents are emitted in real-time as they occur
TRADE_FILL - Provides a stream of trades that have occurred filtered by product.
TradeFillevents are emitted in real-time as they occurSimilar to
OrderFill, an array of trade fills will be emitted in a single message, grouped by the product they were traded on
ORDER_UPDATE - Provides updates about order status changes for a specific subaccount.
OrderUpdateevents are emitted in real-time
Only the latest update processed is emitted and intermediary states are omitted.
SUBACCOUNT_LIQUIDATION - Provides an update when a subaccount is liquidated.
SubaccountLiquidationevents are emitted in real-timesubaccountIdthe subaccount that has been liquidated,liquidatedAtthe time (in ms, since the Unix epoch) when the liquidation occurred
TOKEN_TRANSFER - Updates on token transfers (deposits/withdrawals) for a specific subaccount.
Each subscription requires specific parameters as shown in the formats above. To subscribe to these streams, emit a 'subscribe' event to the socket with the appropriate subscription message.
During connection establishment ensure websocket is the only configured transport (i.e. transports: ['websocket']).
Unsubscribe
To stop receiving data from a previously established subscription, you can unsubscribe using the same payload format as your original subscription. Simply emit an unsubscribe event to the socket with the identical payload structure you used when subscribing.
Handling Socket.io Exceptions
Socket.io ExceptionsExceptions are exposed in its own event aptly named "exception". Exceptions follow the following shape:
patternindicates the source event pattern if available e.g. "subscribe"statusthe error statuserrorgeneral body of the error its shape changes depending on the status
Websocket Subscription Streams
Websocket Subscription StreamsThe Native WebSocket interface provides a streamlined alternative to Socket.IO for subscribing to real-time data streams. Subscriptions follow the same channel-based model but use a simplified payload format with short-form keys for reduced bandwidth and faster parsing.
Below is the initial specification for the upcoming native WebSocket stream. As of writing, native WebSockets are not yet live and this specification is subject to change. Feedback is welcome.
Subscribe
There are few minor differences subscription payloads compared to socket.io .
symbolis used as an identifier instead ofproductIdShort form keys are returned instead
eevent type is returned for customized message handling
L2Book (replaces BOOK_DEPTH ) - Provides order book depth updates for a specific product.
eis the event namesis the symbol e.g. BTCUSDtis the timestamp (epoch in milliseconds)ptis the previous timestamp (epoch in milliseconds) - optionalaare asks, array of[price, qty]pairsbare bids, array of[price, qty]pairs
L2Book is a renaming of the Socket.IO BookDepth stream.
MARKET_PRICE - Delivers real-time market price updates for a specified product.
eis the event namesis the symbol e.g. BTCUSDtis the server timestamp this message was emitted at (epoch in milliseconds)bidPxis the best bid priceaskPxis the best ask pricebidAmtis the total quantity at the best bidaskAmtis the total quantity at the best askmarkPxis the current mark pricemark24hPxis the 24h mark price
MarketPrice fields can be explored with some minor renaming. In extreme cases, MarketPrice will skip publishing if both bidPx & askPx are not available. All numbers are returned as decimals (9 precision).
SUBACCOUNT_LIQUIDATION - Provides an update when a subaccount is liquidated.
When a subaccount is liquidated, all positions are transferred to the insurance fund and derisked at a later time.
p- An array of positions liquidated (subaccount may have one or many positions):priceis the mark price at the time of liquidationszis the position size at liquidation
tis the server timestamp (epoch in milliseconds)sidis theidof liquidated subaccount
ORDER_UPDATE - Provides updates about order status changes for a specific subaccount.
datais the same response type asOrderDtotis the server timestamp (epoch in milliseconds)
Unsubscribe
Similar to subscribe request format we have, instead use unsubscribe as the event type:
Handling Websocket Exceptions
Websocket ExceptionsError responses are returned directly as a reply to the originating event (e.g. subscribe, unsubscribe). They follow a unified shape:
okindicates whether the request succeededcodea machine-readable error code present whenokisfalse
unknown_product
The provided symbol does not match any known product.
validation_error
The request payload failed validation (e.g. missing or invalid fields).
subscription_failed
The server was unable to subscribe to the requested topic.
unsubscribe_failed
The server was unable to unsubscribe from the requested topic.
rate_limit
Too many requests, the client has been rate-limited.
internal_error
An unexpected server-side error occurred.
A successful response would simply just return { "ok": true, ... }
Last updated
