Class: WebsocketClient
Extends
BaseWebsocketClient<WsKey,any>
Constructors
Constructor
new WebsocketClient(options?: WSClientConfigurableOptions, logger?: object): WebsocketClient;Parameters
| Parameter | Type |
|---|---|
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()
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
| Parameter | Type |
|---|---|
wsKey | WsKey |
Returns
Promise<unknown>
Inherited from
BaseWebsocketClient.assertIsAuthenticated
close()
close(wsKey: WsKey, force?: boolean): void;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
force? | boolean |
Returns
void
Inherited from
BaseWebsocketClient.close
closeAll()
closeAll(force?: boolean): void;Parameters
| Parameter | Type |
|---|---|
force? | boolean |
Returns
void
See
Inherited from
BaseWebsocketClient.closeAll
connect()
connect(
wsKey: WsKey,
customUrl?: string,
throwOnError?: boolean
): Promise<WSConnectedResult | undefined>;Request connection to a specific websocket, instead of waiting for automatic connection.
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
customUrl? | string |
throwOnError? | boolean |
Returns
Promise<WSConnectedResult | undefined>
Inherited from
BaseWebsocketClient.connect
connectAll()
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()
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
| Parameter | Type |
|---|---|
wsKey | keyof WSAPIWsKeyTopicMap |
skipAuth? | boolean |
Returns
Promise<unknown>
emit()
emit<U extends keyof WSClientEventMap<TWSKey>>(event: U, ...args: Parameters<WSClientEventMap<WsKey>[U]>): boolean;Type Parameters
| Type Parameter |
|---|
U extends keyof WSClientEventMap<TWSKey> |
Parameters
| Parameter | Type |
|---|---|
event | U |
...args | Parameters<WSClientEventMap<WsKey>[U]> |
Returns
boolean
Inherited from
BaseWebsocketClient.emit
getTimeOffsetMs()
getTimeOffsetMs(): number;Returns
number
Inherited from
BaseWebsocketClient.getTimeOffsetMs
getWsStore()
getWsStore(): WsStore<WsKey, WSTopicRequest<WSTopic, any>>;Get the WsStore that tracks websockets & topics
Returns
WsStore<WsKey, WSTopicRequest<WSTopic, any>>
Inherited from
BaseWebsocketClient.getWsStore
isConnected()
isConnected(wsKey: WsKey): boolean;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
Returns
boolean
Inherited from
BaseWebsocketClient.isConnected
on()
on<U extends keyof WSClientEventMap<TWSKey>>(event: U, listener: WSClientEventMap<WsKey>[U]): this;Type Parameters
| Type Parameter |
|---|
U extends keyof WSClientEventMap<TWSKey> |
Parameters
| Parameter | Type |
|---|---|
event | U |
listener | WSClientEventMap<WsKey>[U] |
Returns
this
Inherited from
BaseWebsocketClient.on
sendWSAPIRequest()
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 Parameter | Default 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
| Parameter | Type | Description |
|---|---|---|
wsKey | TWSKey | Kraken Spot connection to use: spotPrivateV2 or spotBetaPrivateV2. |
operation | TWSOperation | - |
params? | TWSParams extends void ? undefined : TWSParams | Parameters 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()
setTimeOffsetMs(newOffset: number): void;Parameters
| Parameter | Type |
|---|---|
newOffset | number |
Returns
void
Inherited from
BaseWebsocketClient.setTimeOffsetMs
subscribe()
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
| Parameter | Type |
|---|---|
requests | | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any> | ( | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[] |
wsKey | WsKey |
Returns
void
Example
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
tryWsSend()
tryWsSend(
wsKey: WsKey,
wsMessage: string,
throwExceptions?: boolean
): void;Try sending a string event on a WS connection (identified by the WS Key)
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
wsMessage | string |
throwExceptions? | boolean |
Returns
void
Inherited from
BaseWebsocketClient.tryWsSend
unsubscribe()
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
| Parameter | Type |
|---|---|
requests | | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any> | ( | TTopic | WSTopicRequest<TTopic, [TTopic] extends ["book"] ? WSPublicBookParams : any>)[] |
wsKey | WsKey |
Returns
void