Skip to content

Class: WebsocketClient ​

Extends ​

  • BaseWebsocketClient<WsKey, any>

Constructors ​

Constructor ​

ts
new WebsocketClient(options?: WSClientConfigurableOptions, logger?: object): WebsocketClient;

Parameters ​

ParameterType
options?WSClientConfigurableOptions
logger?{ error: (...params: any) => void; info: (...params: any) => void; trace: (..._params: any) => void; }
logger.error?(...params: any) => void
logger.info?(...params: any) => void
logger.trace?(..._params: any) => void

Returns ​

WebsocketClient

Overrides ​

BaseWebsocketClient<WsKey, any>.constructor

Methods ​

assertIsAuthenticated() ​

ts
assertIsAuthenticated(wsKey: WsKey): Promise<unknown>;

Ensure the connection is open and run its authentication setup. For Spot, this does not send a login request or validate credentials; authentication is attached to each subscription or API request.

Parameters ​

ParameterType
wsKeyWsKey

Returns ​

Promise<unknown>

Inherited from ​

BaseWebsocketClient.assertIsAuthenticated


close() ​

ts
close(wsKey: WsKey, force?: boolean): void;

Parameters ​

ParameterType
wsKeyWsKey
force?boolean

Returns ​

void

Inherited from ​

BaseWebsocketClient.close


closeAll() ​

ts
closeAll(force?: boolean): void;

Parameters ​

ParameterType
force?boolean

Returns ​

void

See ​

Shut down cleanly

Inherited from ​

BaseWebsocketClient.closeAll


connect() ​

ts
connect(
   wsKey: WsKey, 
   customUrl?: string, 
   throwOnError?: boolean
): Promise<WSConnectedResult | undefined>;

Request connection to a specific websocket, instead of waiting for automatic connection.

Parameters ​

ParameterType
wsKeyWsKey
customUrl?string
throwOnError?boolean

Returns ​

Promise<WSConnectedResult | undefined>

Inherited from ​

BaseWebsocketClient.connect


connectAll() ​

ts
connectAll(): Promise<WSConnectedResult | undefined>[];

Open the production Spot private v2 connection and return an array containing its connection promise.

Returns ​

Promise<WSConnectedResult | undefined>[]

Overrides ​

BaseWebsocketClient.connectAll


connectWSAPI() ​

ts
connectWSAPI(wsKey: keyof WSAPIWsKeyTopicMap, skipAuth?: boolean): Promise<unknown>;

Open the selected Spot WebSocket API connection before sending requests. Spot authentication is included in each request; this method does not confirm credentials with a login exchange. With skipAuth enabled, only the connection check runs.

Parameters ​

ParameterType
wsKeykeyof WSAPIWsKeyTopicMap
skipAuth?boolean

Returns ​

Promise<unknown>


emit() ​

ts
emit<U extends keyof WSClientEventMap<TWSKey>>(event: U, ...args: Parameters<WSClientEventMap<WsKey>[U]>): boolean;

Type Parameters ​

Type Parameter
U extends keyof WSClientEventMap<TWSKey>

Parameters ​

ParameterType
eventU
...argsParameters<WSClientEventMap<WsKey>[U]>

Returns ​

boolean

Inherited from ​

BaseWebsocketClient.emit


getTimeOffsetMs() ​

ts
getTimeOffsetMs(): number;

Returns ​

number

Inherited from ​

BaseWebsocketClient.getTimeOffsetMs


getWsStore() ​

ts
getWsStore(): WsStore<WsKey, WSTopicRequest<WSTopic, any>>;

Get the WsStore that tracks websockets & topics

Returns ​

WsStore<WsKey, WSTopicRequest<WSTopic, any>>

Inherited from ​

BaseWebsocketClient.getWsStore


isConnected() ​

ts
isConnected(wsKey: WsKey): boolean;

Parameters ​

ParameterType
wsKeyWsKey

Returns ​

boolean

Inherited from ​

BaseWebsocketClient.isConnected


on() ​

ts
on<U extends keyof WSClientEventMap<TWSKey>>(event: U, listener: WSClientEventMap<WsKey>[U]): this;

Type Parameters ​

Type Parameter
U extends keyof WSClientEventMap<TWSKey>

Parameters ​

ParameterType
eventU
listenerWSClientEventMap<WsKey>[U]

Returns ​

this

Inherited from ​

BaseWebsocketClient.on


sendWSAPIRequest() ​

ts
sendWSAPIRequest<TWSKey extends keyof WSAPIWsKeyTopicMap, TWSOperation extends 
  | "add_order"
  | "amend_order"
  | "cancel_order"
  | "cancel_all"
  | "cancel_all_orders_after"
  | "batch_add"
  | "batch_cancel"
  | "edit_order", TWSParams extends { [K in string | number | symbol]: WSAPITopicRequestParamMap[TWSOperation][K] }, TWSAPIResponse extends 
  | object
  | WSAPISpotResponse<WSAPIAddSpotOrderResult, "add_order">
  | WSAPISpotResponse<WSAPIAmendSpotOrderResult, "amend_order">
  | WSAPISpotResponse<WSAPICancelSpotOrderResult, "cancel_order">
  | WSAPISpotResponse<WSAPICancelAllSpotOrdersResult, "cancel_all">
  | WSAPISpotResponse<WSAPICancelAllSpotOrdersAfterResult, "cancel_all_orders_after">
  | WSAPISpotResponse<WSAPIBatchAddSpotOrdersResult[], "batch_add">
  | WSAPISpotResponse<WSAPIBatchCancelSpotOrdersResult, "batch_cancel">
  | WSAPISpotResponse<WSAPIEditSpotOrderResult, "edit_order"> = WSAPITopicResponseMap[TWSOperation]>(
   wsKey: TWSKey, 
   operation: TWSOperation, 
   params?: TWSParams extends void ? undefined : TWSParams, 
   requestFlags?: WSAPIRequestFlags
): Promise<TWSAPIResponse>;

Send a Kraken Spot WebSocket API request and return a promise for its response. The SDK opens the selected connection, obtains a Spot WebSocket token and includes it in the request automatically; do not send a separate login request. The promise rejects on an error response or a lost connection. A rejected request may already have been processed, so resolve uncertain order outcomes before retrying.

Type Parameters ​

Type ParameterDefault type
TWSKey extends keyof WSAPIWsKeyTopicMap-
TWSOperation extends | "add_order" | "amend_order" | "cancel_order" | "cancel_all" | "cancel_all_orders_after" | "batch_add" | "batch_cancel" | "edit_order"-
TWSParams extends { [K in string | number | symbol]: WSAPITopicRequestParamMap[TWSOperation][K] }-
TWSAPIResponse extends | object | WSAPISpotResponse<WSAPIAddSpotOrderResult, "add_order"> | WSAPISpotResponse<WSAPIAmendSpotOrderResult, "amend_order"> | WSAPISpotResponse<WSAPICancelSpotOrderResult, "cancel_order"> | WSAPISpotResponse<WSAPICancelAllSpotOrdersResult, "cancel_all"> | WSAPISpotResponse<WSAPICancelAllSpotOrdersAfterResult, "cancel_all_orders_after"> | WSAPISpotResponse<WSAPIBatchAddSpotOrdersResult[], "batch_add"> | WSAPISpotResponse<WSAPIBatchCancelSpotOrdersResult, "batch_cancel"> | WSAPISpotResponse<WSAPIEditSpotOrderResult, "edit_order">WSAPITopicResponseMap[TWSOperation]

Parameters ​

ParameterTypeDescription
wsKeyTWSKeyKraken Spot connection to use: spotPrivateV2 or spotBetaPrivateV2.
operationTWSOperation-
params?TWSParams extends void ? undefined : TWSParamsParameters for the selected Kraken operation, sent in the request's params object. The SDK supplies the authentication token automatically.
requestFlags?WSAPIRequestFlags-

Returns ​

Promise<TWSAPIResponse>

Promise - tries to resolve with async WS API response. Rejects if disconnected or exception is seen in async WS API response

Overrides ​

BaseWebsocketClient.sendWSAPIRequest


setTimeOffsetMs() ​

ts
setTimeOffsetMs(newOffset: number): void;

Parameters ​

ParameterType
newOffsetnumber

Returns ​

void

Inherited from ​

BaseWebsocketClient.setTimeOffsetMs


subscribe() ​

ts
subscribe<TTopic extends WSTopic>(requests: 
  | TTopic
  | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>
  | (
  | TTopic
  | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[], wsKey: WsKey): void;

Subscribe to one or more topics. Pass a topic string, an object with topic and optional payload, or an array of these values. Supply the connection key as the second argument. The SDK connects, authenticates when needed and restores subscriptions after reconnecting. Call unsubscribe(requests, wsKey) with the same requests and connection key to remove them.

Type Parameters ​

Type Parameter
TTopic extends WSTopic

Parameters ​

ParameterType
requests| TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any> | ( | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[]
wsKeyWsKey

Returns ​

void

Example ​

ts
import { WebsocketClient, WS_KEY_MAP } from '@siebly/kraken-api';

const ws = new WebsocketClient({});

ws.on('message', (message) => console.log(message));
ws.on('response', (response) =>
  console.log('Subscription response:', response),
);
ws.on('exception', (error) => console.error('WebSocket exception:', error));
ws.on('reconnecting', (event) => console.log('Reconnecting:', event));
ws.on('reconnected', (event) => console.log('Reconnected:', event));

ws.subscribe(
  { topic: 'ticker', payload: { symbol: ['BTC/USD'] } },
  WS_KEY_MAP.spotPublicV2,
);

const timer = setTimeout(stop, 30000);
function stop() {
  clearTimeout(timer);
  ws.closeAll();
}
process.once('SIGINT', stop);
process.once('SIGTERM', stop);

See ​

Subscribe to market data


tryWsSend() ​

ts
tryWsSend(
   wsKey: WsKey, 
   wsMessage: string, 
   throwExceptions?: boolean
): void;

Try sending a string event on a WS connection (identified by the WS Key)

Parameters ​

ParameterType
wsKeyWsKey
wsMessagestring
throwExceptions?boolean

Returns ​

void

Inherited from ​

BaseWebsocketClient.tryWsSend


unsubscribe() ​

ts
unsubscribe<TTopic extends WSTopic>(requests: 
  | TTopic
  | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>
  | (
  | TTopic
  | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[], wsKey: WsKey): void;

Unsubscribe from one or more topics on the connection selected by wsKey. The topics are removed from the subscription cache and will not be restored after reconnecting.

Type Parameters ​

Type Parameter
TTopic extends WSTopic

Parameters ​

ParameterType
requests| TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any> | ( | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[]
wsKeyWsKey

Returns ​

void

Built by Siebly. Open source SDKs for exchange APIs.