• 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 KatanaPerps<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 KatanaPerpsMessageEvent,
type KatanaPerpsTickerEventData,
type KatanaPerpsSubscribeType,
} from '@katanaperps/katana-perps-sdk';

const client = new WebSocketClient();

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

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

client.onMessage((message: KatanaPerpsMessageEvent) => {
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 KatanaPerps<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 KatanaPerpsOrderEvent {
    type: "orders";
    data: KatanaPerpsOrderEventData;
}

Hierarchy (view full)

Properties

Properties

type: "orders"

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

Order updates received from the WebSocket differ from orders retreived from the REST API in several ways.

  • In addition to the order types received when getting orders from the REST API, WebSocket update events may also provide the following undefined type indicating a KatanaPerpsOrderEventDataSystemFill where the fills property will include a FillTypeSystem fill matching KatanaPerpsOrderFillEventDataSystem
  • It is best to narrow on the type property between these types and all the others as shown in the example below.
    • This is made easiest by using the OrderType enum as shown.

Example

 import { OrderEventType } from '@katanaperps/katana-perps-sdk';

// ...

const orderEventData = orderEvent.data

if (
orderEventData.type === OrderEventType.liquidation ||
orderEventData.type === OrderEventType.deleverage
) {
// orderEventData is of type KatanaPerpsOrderEventDataLiquidation | KatanaPerpsOrderEventDataDeleverage
} else {
// orderEventData is of type KatanaPerpsOrderEventDataGeneral
}


See

type KatanaPerpsOrderEventData