Socket Client
Getting Started
Installation
pnpm install @dydxprotocol/v4-client-js
Initializing the Client
const mySocket = new SocketClient(
Network.staging().indexerConfig,
() => {
console.log('socket opened');
},
() => {
console.log('socket closed');
},
(message) => {
console.log(message);
},
);
mySocket.connect();
Subscription
Markets Channel
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToMarkets();
// unsubscribe
mySocket.unsubscribeFromMarkets()
Response and Channel Data: See Indexer Socket for Initial Response and Channel Update
Trades Channel
// ticker is the market ticker, such as "BTC-USD"
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToTrades(ticker);
// unsubscribe
mySocket.unsubscribeFromTrades(ticker)
Response and Channel Data: See Indexer Socket for Initial Response and Channel Update
Orderbook Channel
// ticker is the market ticker, such as "BTC-USD"
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToOrderbook(ticker);
// unsubscribe
mySocket.unsubscribeFromOrderbook(ticker)
Response and Channel Data: See Indexer Socket for Initial Response and Channel Update
Candles Channel
// ticker is the market ticker, such as "BTC-USD"; resolution is the candles resolution
// subscribe
// updates are batched, received in channel_batch_data
mySocket.subscribeToCandles(ticker, resolution);
// unsubscribe
mySocket.unsubscribeFromCandles(ticker, resolution)
Response and Channel Data: See Indexer Socket for Initial Response and Channel Update
Subaccount Channel
// address is the wallet address on dYdX chain, subaccount_number is the subaccount number
// subscribe
// updates are not batched, received in channel_data
mySocket.subscribeToSubaccount(ticker, resolution);
// unsubscribe
mySocket.unsubscribeFromSubaccount(ticker, resolution)
Response and Channel Data: See Indexer Socket for Initial Response and Channel Update