Interface IDEXSubscriptionEventBase

  • subscription Websocket messages always include a type property which dictates the shape of its data property.
    • type will be the name of the subscription that the message correlates to.
    • Each IDEX<name>Event interface will document its provided data shape and properties.
  • subscriptions type messages will instead have a subscriptions property when dispatched.
  • Tip: Using the enum MessageEventType will make it easier to narrow the messages.
import {
WebSocketClient,
MessageEventType,
SubscriptionNamePublic,
type IDEXMessageEvent,
type IDEXTickerEventData,
type IDEXSubscribeType,
} from '@idexio/idex-sdk';

const client = new WebSocketClient();

function handleSubscriptions(subscriptions: IDEXSubscribeType[]) {
// all subscribed subscriptions
console.log('Active Subscriptions: ', subscriptions);
}

function handleTickersUpdate(message: IDEXTickerEventData) {
// handle the updated data
console.log('Ticker: ', message);
}

client.onMessage((message: IDEXMessageEvent) => {
switch (message.type) {
case MessageEventType.error:
console.error(
`[${message.data.code}] | Error received from WebSocketServer: ${message.data.message}`,
);
break;
case MessageEventType.subscriptions:
return handleSubscriptions(message.subscriptions);
// narrows the type to the specific IDEX<name>Event type
case MessageEventType.tickers:
return handleTickersUpdate(message.data);
default:
break;
}
});

async function main() {
await client.connect();

client.subscribePublic([
{
name: SubscriptionNamePublic.tickers,
markets: ['ETH-USD'],
},
]);
}

main().catch((error) => {
console.error('Error During Startup: ', error);
});
interface IDEXSubscriptionEventBase {
    type: "deposits" | "fundingPayments" | "orders" | "positions" | "withdrawals" | "webclient" | "tickers" | "trades" | "liquidations" | "candles" | "l1orderbook" | "l2orderbook";
    data: object;
}

Hierarchy (view full)

Properties

Properties

type: "deposits" | "fundingPayments" | "orders" | "positions" | "withdrawals" | "webclient" | "tickers" | "trades" | "liquidations" | "candles" | "l1orderbook" | "l2orderbook"

The type property is used to determine the shape of the data property of the update event.

  • Subscription update events will match IDEXSubscriptionEventBase with the subscription name as the value for the type property.
  • Otherwise the type will be:
    • subscriptions if you requested a list of all current subscriptions
    • error if your request resulted in a server error
data: object

The data object will change based on the type of the message.