Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 1.63 KB

File metadata and controls

54 lines (37 loc) · 1.63 KB

WebSocket Guide

This guide demonstrates how to use WebSockets with the dYdX Python SDK, including a simple subscription example and a more advanced basic adder example

Setting Up

The websocket indexer allows to subscribe to channels to obtain live updates: First, import the necessary modules:

import asyncio
from dydx_v4_client.indexer.socket.websocket import IndexerSocket, CandlesResolution
from dydx_v4_client.network import TESTNET

# Replace with your actual address
TEST_ADDRESS_3 = "your_address_here"
ETH_USD = "ENA-USD"

Simple Subscription Example

Here's a simple example of how to subscribe to different channels:

def handle_message(ws: IndexerSocket, message: dict):
    print("Received message:", message)

async def simple_subscription():
    async with IndexerSocket(TESTNET.websocket_indexer, on_message=handle_message) as ws:
        # Subscribe to markets
        await ws.markets.subscribe()
        
        # Subscribe to orderbook for ENA-USD
        await ws.order_book.subscribe(ETH_USD)
        
        # Subscribe to trades for ENA-USD
        await ws.trades.subscribe(ETH_USD)
        
        # Subscribe to 15-minute candles for ENA-USD
        await ws.candles.subscribe(ETH_USD, CandlesResolution.FIFTEEN_MINUTES)
        
        # Subscribe to a specific subaccount
        await ws.subaccounts.subscribe(TEST_ADDRESS_3, 0)
        
        # Keep the connection alive
        while True:
            await asyncio.sleep(1)

asyncio.run(simple_subscription())

Advanced Example: Basic Adder

You can find a more advanced example in the ./examples/basic_adder.py file.