Class: WebsocketClient
Extends
BaseWebsocketClient<WsKey,WsRequestOperationOKX<object>>
Constructors
Constructor
new WebsocketClient(options?: WSClientConfigurableOptions, logger?: object): WebsocketClient;Parameters
| Parameter | Type | Description |
|---|---|---|
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 | Ping/pong events and other raw messages that might be noisy. Enable this while troubleshooting. |
Returns
WebsocketClient
Properties
logger
logger: object;error
error: (...params: any) => void;Parameters
| Parameter | Type |
|---|---|
...params | any |
Returns
void
info
info: (...params: any) => void;Parameters
| Parameter | Type |
|---|---|
...params | any |
Returns
void
trace
trace: (..._params: any) => void;Ping/pong events and other raw messages that might be noisy. Enable this while troubleshooting.
Parameters
| Parameter | Type |
|---|---|
..._params | any |
Returns
void
Methods
assertIsAuthenticated()
assertIsAuthenticated(wsKey: WsKey, skipPublicWsKeyCheck: boolean): Promise<unknown>;Promise-driven method to assert that a ws has been successfully authenticated (will await until auth is confirmed)
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
skipPublicWsKeyCheck | boolean |
Returns
Promise<unknown>
assertIsConnected()
assertIsConnected(wsKey: WsKey): Promise<unknown>;Promise-driven method to assert that a ws has successfully connected (will await until connection is open)
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
Returns
Promise<unknown>
close()
close(wsKey: WsKey, force?: boolean): void;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
force? | boolean |
Returns
void
closeAll()
closeAll(force?: boolean): void;Parameters
| Parameter | Type |
|---|---|
force? | boolean |
Returns
void
See
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>
connectAll()
connectAll(): Promise<WSConnectedResult | undefined>[];Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library
Returns
Promise<WSConnectedResult | undefined>[]
connectPrivate()
connectPrivate(businessEndpoint?: boolean): Promise<WSConnectedResult | undefined>;Parameters
| Parameter | Type |
|---|---|
businessEndpoint? | boolean |
Returns
Promise<WSConnectedResult | undefined>
connectPublic()
connectPublic(businessEndpoint?: boolean): Promise<WSConnectedResult | undefined>;Parameters
| Parameter | Type |
|---|---|
businessEndpoint? | boolean |
Returns
Promise<WSConnectedResult | undefined>
connectWSAPI()
connectWSAPI(): Promise<unknown[]>;Ensures the WS API connection is active and ready.
You do not need to call this, but if you call this before making any WS API requests, it can accelerate the first request (by preparing the connection in advance).
Returns
Promise<unknown[]>
emit()
emit<U extends keyof WSClientEventMap<WsKey, WsDataEvent<any>>>(event: U, ...args: Parameters<WSClientEventMap<WsKey, any>[U]>): boolean;Type Parameters
| Type Parameter |
|---|
U extends keyof WSClientEventMap<WsKey, WsDataEvent<any>> |
Parameters
| Parameter | Type |
|---|---|
event | U |
...args | Parameters<WSClientEventMap<WsKey, any>[U]> |
Returns
boolean
executeReconnectableClose()
executeReconnectableClose(wsKey: WsKey, reason: string): void;Closes a connection, if it's even open. If open, this will trigger a reconnect asynchronously. If closed, trigger a reconnect immediately
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
reason | string |
Returns
void
getCachedMidFlightRequest()
getCachedMidFlightRequest(wsKey: WsKey, requestKey: string):
| WsRequestOperationOKX<object>
| undefined;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
requestKey | string |
Returns
| WsRequestOperationOKX<object> | undefined
getMarketWsKey()
getMarketWsKey(type: "private" | "business"): WsKey;Internal methods required to integrate with the BaseWSClient
Parameters
| Parameter | Type |
|---|---|
type | "private" | "business" |
Returns
getTimeOffsetMs()
getTimeOffsetMs(): number;Returns
number
getWsStore()
getWsStore(): WsStore<WsKey, WsTopicRequest<string, unknown>>;Get the WsStore that tracks websockets & topics
Returns
WsStore<WsKey, WsTopicRequest<string, unknown>>
isConnected()
isConnected(wsKey: WsKey): boolean;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
Returns
boolean
on()
on<U extends keyof WSClientEventMap<WsKey, WsDataEvent<any>>>(event: U, listener: WSClientEventMap<WsKey, any>[U]): this;Type Parameters
| Type Parameter |
|---|
U extends keyof WSClientEventMap<WsKey, WsDataEvent<any>> |
Parameters
| Parameter | Type |
|---|---|
event | U |
listener | WSClientEventMap<WsKey, any>[U] |
Returns
this
removeCachedMidFlightRequest()
removeCachedMidFlightRequest(wsKey: WsKey, requestKey: string): void;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
requestKey | string |
Returns
void
sendWSAPIRequest()
sendWSAPIRequest<TWSKey extends keyof WsAPIWsKeyTopicMap, TWSOperation extends
| WSAPIPrivateOperations
| WSAPIBusinessOperations, TWSParams extends
| OrderIdRequest
| WSAPIPlaceOrderRequestV5
| WSAPIAmendOrderRequestV5
| WSAPIMassCancelOrdersRequestV5
| WSAPIPlaceSpreadOrderRequestV5
| WSAPIAmendSpreadOrderRequestV5
| WSAPICancelSpreadOrderRequestV5
| WSAPISpreadMassCancelOrdersRequestV5
| WSAPIPlaceOrderRequestV5[]
| OrderIdRequest[]
| WSAPIAmendOrderRequestV5[], TWSAPIResponse extends
| WSAPIResponse<[OrderResult], "order">
| WSAPIResponse<OrderResult[], "batch-orders">
| WSAPIResponse<[WSAPICancelOrderResultV5], "cancel-order">
| WSAPIResponse<WSAPICancelOrderResultV5[], "batch-cancel-orders">
| WSAPIResponse<[OrderResult], "amend-order">
| WSAPIResponse<[OrderResult], "batch-amend-orders">
| WSAPIResponse<[{
result: boolean;
}], "mass-cancel">
| WSAPIResponse<[WSAPISpreadPlaceOrderResultV5], "sprd-order">
| WSAPIResponse<[WSAPISpreadAmendOrderResultV5], "sprd-amend-order">
| WSAPIResponse<[WSAPISpreadCancelOrderResultV5], "sprd-cancel-order">
| WSAPIResponse<[{
result: boolean;
}], "sprd-mass-cancel"> = WsAPIOperationResponseMap[TWSOperation]>(
rawWsKey: WsKey,
operation: TWSOperation,
params: TWSParams & object,
requestFlags?: WSAPIRequestFlags
): Promise<TWSAPIResponse>;OKX supports order placement via WebSockets. This is the WS API: https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-place-order
For convenient promise-wrapped usage of the WS API, instance the WebsocketAPIClient class exported by this SDK.
For demo trading, set demoTrading:true in the WS Client config.
Type Parameters
Parameters
| Parameter | Type |
|---|---|
rawWsKey | WsKey |
operation | TWSOperation |
params | TWSParams & object |
requestFlags? | WSAPIRequestFlags |
Returns
Promise<TWSAPIResponse>
a promise that resolves/rejects when a matching response arrives
setTimeOffsetMs()
setTimeOffsetMs(newOffset: number): void;Parameters
| Parameter | Type |
|---|---|
newOffset | number |
Returns
void
subscribe()
subscribe(wsEvents:
| WsChannelSubUnSubRequestArg
| WsChannelSubUnSubRequestArg[], isPrivateTopic?: boolean): Promise<unknown>[];Subscribe to topics & track/persist them. They will be automatically resubscribed to if the connection drops/reconnects.
Parameters
| Parameter | Type | Description |
|---|---|---|
wsEvents | | WsChannelSubUnSubRequestArg | WsChannelSubUnSubRequestArg[] | topic or list of topics |
isPrivateTopic? | boolean | optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) |
Returns
Promise<unknown>[]
See
Subscribe to public market data
tryWsSend()
tryWsSend(
wsKey: WsKey,
wsMessage: string,
throwExceptions?: boolean
): void;Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
wsMessage | string |
throwExceptions? | boolean |
Returns
void
unsubscribe()
unsubscribe(wsEvents:
| WsChannelSubUnSubRequestArg
| WsChannelSubUnSubRequestArg[], isPrivateTopic?: boolean): Promise<unknown>[];Unsubscribe from topics & remove them from memory. They won't be re-subscribed to if the connection reconnects.
Parameters
| Parameter | Type | Description |
|---|---|---|
wsEvents | | WsChannelSubUnSubRequestArg | WsChannelSubUnSubRequestArg[] | - |
isPrivateTopic? | boolean | optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) |
Returns
Promise<unknown>[]