Example: privateWs.ts
Static snapshot for Kraken/Derivatives/WebSockets/privateWs.ts.
Example Path
Kraken/Derivatives/WebSockets/privateWs.ts
Source Link
Repository source: https://github.com/sieblyio/crypto-api-examples/blob/master/examples/Kraken/Derivatives/WebSockets/privateWs.ts
Code Snapshot
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import {
DefaultLogger,
LogParams,
WebsocketClient,
WS_KEY_MAP,
WSTopicRequest,
} from '@siebly/kraken-api';
import { WSDerivativesTopic } from '@siebly/kraken-api';
const customLogger: DefaultLogger = {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
trace: (...params: LogParams): void => {
// console.log(new Date(), '--> trace', ...params);
},
info: (...params: LogParams): void => {
console.log(new Date(), '--> info', ...params);
},
error: (...params: LogParams): void => {
console.error(new Date(), '--> error', ...params);
},
};
async function start() {
const account = {
key: process.env.API_FUTURES_KEY || 'keyHere',
secret: process.env.API_FUTURES_SECRET || 'secretHere',
};
/**
* The WebsocketClient is the core class to manage WebSocket subscriptions. Give it the topics you want to subscribe to, and it will handle the rest:
* - Connection management (connect, disconnect, reconnect)
* - Authentication for private topics
* - Subscription management (subscribe, unsubscribe, resubscribe on reconnect)
* - Message handling (dispatch messages to appropriate handlers)
*
* All you need to do is provide the topics you want to subscribe to when calling `subscribe()`, and the client will take care of the rest.
*
* Here we create a WebsocketClient instance with API key/secret for private topic subscriptions.
*
* In terms of product groups such as Spot, Derivatives, etc., the WebsocketClient understand the product group from the WsKey you provide when subscribing. For example, using `WS_KEY_MAP.spotPrivateV2` indicates that the subscription is for Spot private topics, as shown below.
*
* Refer to WS_KEY_MAP in the source code for all available WsKey options.
*/
const client = new WebsocketClient(
{
apiKey: account.key,
apiSecret: account.secret,
},
customLogger,
);
client.on('open', (data) => {
console.log(new Date(), 'connected ', data?.wsKey);
});
// Data received
client.on('message', (data) => {
console.info(new Date(), 'data received: ', JSON.stringify(data));
});
// Something happened, attempting to reconnect
client.on('reconnecting', (data) => {
console.log(new Date(), 'reconnect: ', data?.wsKey);
});
// Reconnect successful
client.on('reconnected', (data) => {
console.log(new Date(), 'reconnected: ', data?.wsKey);
});
// Connection closed. If unexpected, expect reconnect -> reconnected.
client.on('close', (data) => {
console.error(new Date(), 'close: ', data);
});
// Reply to a request, e.g. "subscribe"/"unsubscribe"/"authenticate"
client.on('response', (data) => {
console.info(new Date(), 'server reply: ', JSON.stringify(data), '\n');
});
client.on('exception', (data) => {
console.error(new Date(), 'exception: ', data);
});
client.on('authenticated', (data) => {
console.error(new Date(), 'authenticated: ', data);
});
/**
* The below examples demonstrate how you can subscribe to private topics.
*
* Note: while the documentation specifies "api_key", "original_challenge" and "signed_challenge" as required parameters, but don't worry about that. The SDK will automatically:
* - Fetch the challenge using your API key,
* - Cache the challenge
* - Include the key, original challenge and signed challenge parameters for you when subscribing to private topics on the derivativesPrivateV1 WebSocket connection.
*
* You do NOT need to manually fetch or provide the "original_challenge" and "signed_challenge" tokens when subscribing to private topics.
*
* Do note that all of these include the "derivativesPrivateV1" WsKey reference. This tells the WebsocketClient to use the private "wss://futures.kraken.com/ws/v1" endpoint for these private subscription requests. It will also automatically authenticate the connection when it is established.
*/
try {
/**
* All of the following parameters require API keys for Derivatives APIs.
*
* Note: your "WsTopicRequest" does not need to include "api_key", "original_challenge" and "signed_challenge". See above for details, or below for examples.
*/
// Open orders: https://docs.kraken.com/api/docs/futures-api/websocket/open_orders
const openOrdersTopicRequest: WSTopicRequest<WSDerivativesTopic> = {
topic: 'open_orders',
};
client.subscribe(openOrdersTopicRequest, WS_KEY_MAP.derivativesPrivateV1);
// Note: if there are no parameters needed, you can also just request the topic by name
// This is the same as openOrdersTopicRequest, since openOrdersTopicRequest contains no parameters
// client.subscribe('open_orders', WS_KEY_MAP.derivativesPrivateV1);
// Open orders (verbose): https://docs.kraken.com/api/docs/futures-api/websocket/open_orders
client.subscribe('open_orders_verbose', WS_KEY_MAP.derivativesPrivateV1);
// Fills: https://docs.kraken.com/api/docs/futures-api/websocket/fills
const accountFillsTopicRequest: WSTopicRequest<WSDerivativesTopic> = {
topic: 'fills',
// Optionally, the product_ids field can be used to subscribe only to specific product.
// payload: {
// product_ids: ['PF_XBTUSD'],
// },
};
client.subscribe(accountFillsTopicRequest, WS_KEY_MAP.derivativesPrivateV1);
// Balances: https://docs.kraken.com/api/docs/futures-api/websocket/balances
client.subscribe('balances', WS_KEY_MAP.derivativesPrivateV1);
// Open Position: https://docs.kraken.com/api/docs/futures-api/websocket/open_position
client.subscribe('open_positions', WS_KEY_MAP.derivativesPrivateV1);
// Account Log: https://docs.kraken.com/api/docs/futures-api/websocket/account_log
client.subscribe('account_log', WS_KEY_MAP.derivativesPrivateV1);
// Notification: https://docs.kraken.com/api/docs/futures-api/websocket/notifications
client.subscribe('notifications_auth', WS_KEY_MAP.derivativesPrivateV1);
} catch (e) {
console.error('Req error: ', e);
}
}
start();
This is a static, crawlable snapshot. The interactive app loads after JavaScript starts and can refresh live data.