WebSockets
The Indexer can provide realtime data through its WebSockets endpoint.
Below an example is provided of how to establish a connection and watch realtime trades updates. See the full API specification here for other data feeds.
Connect
To get realtime updates, we first need to establish a connection with the WebSockets endpoint.
# The message handler, triggered when a message is received.
def handler(ws: IndexerSocket, message: dict):
print(message)
Upon a successful connection you will receive an initial connection message. This message maybe abstracted away, depending on the client.
Connection response
{
"type": "connected",
"connection_id": "004a1efa-21bb-4b19-a2e9-a8ffadd6dc53",
"message_id": 0
}
Subscribe
After a connection is established, you may subscribe to several feeds, containing different types of data. WebSockets include information on markets, trades, orders, candles, and subaccounts.
# Modify the `handler()` function.
# Subscribe only after a succesful connection.
def handler(ws: IndexerSocket, message: dict):
if message["type"] == "connected":
# Subscribe.
ws.trades.subscribe(ETH_USD)
print(message)
Handling the data
After subscription, you will start receiving the update messages.
Here, the update messages contain the finalized trades (matched orders) for the ETH-USD
ticker.
For each received message, the handler()
function will be called on it. Modify it to implement your desired logic.
Handling in Rust
In Rust, callbacks are not used. Intead, the handle returned on subscription must be polled.
// Continuous loop running until the feed is stopped.
while let Some(msg) = trades_feed.recv().await {
println!("New trades update: {msg:?}");
}
Unsubscribe
When the data feed is not needed anymore, you may stop it and unsubscribe from it.
ws.trades.unsubscribe(ETH_USD)
Details
Rate Limiting
The default rate limiting config for WebSockets is:
- 2 subscriptions per (connection + channel + channel ID) per second.
- 2 invalid messages per connection per second.
Maintaining a Connection
Every 30 seconds, the WebSockets API will send a heartbeat ping
control frame to the connected client.
If a pong
event is not received within 10 seconds back, the websocket API will disconnect.
CLI example
You can use a command-line WebSockets client such as interactive-websocket-cli
to connect and subscribe to channels.
Example (with interactive-websocket-cli
):
# For the deployment by DYDX token holders (mainnet), use
# wscli connect wss://indexer.dydx.trade/v4/ws
wscli connect wss://indexer.v4testnet.dydx.exchange/v4/ws
<output from ws-cli>
<type 's' to send> { "type": "subscribe", "channel": "v4_trades", "id": "BTC-USD" }