Class: WebsocketClientV1
Deprecated
This legacy websocket client creates one websocket connection per topic.
If subscribing to a lot of topics, consider using the new multiplex WebsocketClient.
To split your topics into smaller groups (one connection per group), simply make multiple multiplex WebsocketClient instances.
Extends
EventEmitter
Constructors
Constructor
new WebsocketClientV1(options: WSClientConfigurableOptions, logger?: object): WebsocketClientV1;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
WebsocketClientV1
Methods
close()
close(wsKey: string, shouldReconnectAfterClose?: boolean): void;Parameters
| Parameter | Type |
|---|---|
wsKey | string |
shouldReconnectAfterClose? | boolean |
Returns
void
closeAll()
closeAll(shouldReconnectAfterClose?: boolean): void;Parameters
| Parameter | Type |
|---|---|
shouldReconnectAfterClose? | boolean |
Returns
void
closeWs()
closeWs(ws: WebSocket, shouldReconnectAfterClose?: boolean): void;Parameters
| Parameter | Type |
|---|---|
ws | WebSocket |
shouldReconnectAfterClose? | boolean |
Returns
void
connectToWsUrl()
connectToWsUrl(
url: string,
wsKey?: string,
forceNewConnection?: boolean
): WebSocket;Parameters
| Parameter | Type |
|---|---|
url | string |
wsKey? | string |
forceNewConnection? | boolean |
Returns
WebSocket
getWs()
getWs(wsKey: string): WebSocket | undefined;Parameters
| Parameter | Type |
|---|---|
wsKey | string |
Returns
WebSocket | undefined
on()
Call Signature
on(event: "reply", listener: (event: WsResponse) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "reply" |
listener | (event: WsResponse) => void |
Returns
this
Call Signature
on(event: "message", listener: (event: WsRawMessage) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "message" |
listener | (event: WsRawMessage) => void |
Returns
this
Call Signature
on(event: "formattedMessage", listener: (event: WsFormattedMessage) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "formattedMessage" |
listener | (event: WsFormattedMessage) => void |
Returns
this
Call Signature
on(event: "formattedUserDataMessage", listener: (event: WsUserDataEvents) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "formattedUserDataMessage" |
listener | (event: WsUserDataEvents) => void |
Returns
this
Call Signature
on(event: "error", listener: (event: object) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "error" |
listener | (event: object) => void |
Returns
this
Call Signature
on(event: "open" | "reconnected", listener: (event: object) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "open" | "reconnected" |
listener | (event: object) => void |
Returns
this
Call Signature
on(event: "close" | "reconnecting", listener: (event: object) => void): this;Parameters
| Parameter | Type |
|---|---|
event | "close" | "reconnecting" |
listener | (event: object) => void |
Returns
this
requestGetProperty()
requestGetProperty(
wsKey: WsKey,
property: string,
requestId: number
): void;Send WS message to get property state
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
property | string |
requestId | number |
Returns
void
requestListSubscriptions()
requestListSubscriptions(wsKey: WsKey, requestId: number): void;Send WS message to unsubscribe from topics.
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
requestId | number |
Returns
void
requestSetProperty()
requestSetProperty(
wsKey: WsKey,
property: string,
value: any,
requestId: number
): void;Send WS message to set property state
Parameters
| Parameter | Type |
|---|---|
wsKey | WsKey |
property | string |
value | any |
requestId | number |
Returns
void
subscribeAggregateTrades()
subscribeAggregateTrades(
symbol: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to aggregate trades for a symbol in a market category
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeAll24hrTickers()
subscribeAll24hrTickers(market: "spot" | "usdm" | "coinm", forceNewConnection?: boolean): WebSocket;Subscribe to 24hr ticker in any market.
Parameters
| Parameter | Type |
|---|---|
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeAllBookTickers()
subscribeAllBookTickers(market: "spot" | "usdm" | "coinm", forceNewConnection?: boolean): WebSocket;Subscribe to best bid/ask for all symbols in spot markets.
Parameters
| Parameter | Type |
|---|---|
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeAllLiquidationOrders()
subscribeAllLiquidationOrders(market: "usdm" | "coinm", forceNewConnection?: boolean): WebSocket;Subscribe to best bid/ask for all symbols in spot markets.
Parameters
| Parameter | Type |
|---|---|
market | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeAllMarketMarkPrice()
subscribeAllMarketMarkPrice(
market: "usdm" | "coinm",
updateSpeedMs?: 1000 | 3000,
forceNewConnection?: boolean
): WebSocket;Subscribe to mark price for all symbols in a market category
Parameters
| Parameter | Type | Default value |
|---|---|---|
market | "usdm" | "coinm" | undefined |
updateSpeedMs? | 1000 | 3000 | 3000 |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeAllMini24hrTickers()
subscribeAllMini24hrTickers(market: "spot" | "usdm" | "coinm", forceNewConnection?: boolean): WebSocket;Subscribe to mini 24hr mini ticker in market category.
Parameters
| Parameter | Type |
|---|---|
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeAllRollingWindowTickers()
subscribeAllRollingWindowTickers(
market: "spot",
windowSize: "1h" | "4h" | "1d",
forceNewConnection?: boolean
): WebSocket;Subscribe to rolling window ticker statistics for all market symbols, computed over multiple windows. Note that only tickers that have changed will be present in the array.
Notes:
- Supported window sizes: 1h,4h,1d.
- Supported markets: spot
Parameters
| Parameter | Type |
|---|---|
market | "spot" |
windowSize | "1h" | "4h" | "1d" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeBlockTrades()
subscribeBlockTrades(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to block trades for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeCoinFuturesUserDataStream()
subscribeCoinFuturesUserDataStream(
isTestnet?: boolean,
forceNewConnection?: boolean,
isReconnecting?: boolean
): Promise<WebSocket | undefined>;Subscribe to COIN-M Futures user data stream - listen key is automatically generated.
Parameters
| Parameter | Type |
|---|---|
isTestnet? | boolean |
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
Promise<WebSocket | undefined>
subscribeCoinIndexPrice()
subscribeCoinIndexPrice(
symbol: string,
_updateSpeedMs?: 1000 | 3000,
forceNewConnection?: boolean
): WebSocket;Subscribe to coin index for a symbol in COINM Futures markets _updateSpeedMs is ignored (<pair>@indexPrice@1s was removed). Drop this arg in the next major.
Parameters
| Parameter | Type | Default value |
|---|---|---|
symbol | string | undefined |
_updateSpeedMs? | 1000 | 3000 | 3000 |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeContinuousContractKlines()
subscribeContinuousContractKlines(
symbol: string,
contractType: "perpetual" | "current_quarter" | "next_quarter",
interval: KlineInterval,
market: "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to continuous contract klines(candles) for a symbol futures
Parameters
| Parameter | Type |
|---|---|
symbol | string |
contractType | "perpetual" | "current_quarter" | "next_quarter" |
interval | KlineInterval |
market | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeContractInfoStream()
subscribeContractInfoStream(market: "usdm" | "coinm", forceNewConnection?: boolean): WebSocket;Subscribe to best bid/ask for all symbols in spot markets.
Parameters
| Parameter | Type |
|---|---|
market | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeDiffBookDepth()
subscribeDiffBookDepth(
symbol: string,
updateMs?: 100 | 250 | 500 | 1000,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to orderbook depth updates to locally manage an order book.
Note that the updatems parameter depends on which market you're trading
- Spot: https://binance-docs.github.io/apidocs/spot/en/#diff-depth-stream
- USDM Futures: https://binance-docs.github.io/apidocs/futures/en/#diff-book-depth-streams
Use getContextFromWsKey(data.wsKey) to extract symbol from events
Parameters
| Parameter | Type | Default value |
|---|---|---|
symbol | string | undefined |
updateMs? | 100 | 250 | 500 | 1000 | 100 |
market | "spot" | "usdm" | "coinm" | undefined |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeEndpoint()
subscribeEndpoint(
endpoint: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to a universal market websocket stream
Parameters
| Parameter | Type |
|---|---|
endpoint | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeIndexKlines()
subscribeIndexKlines(
symbol: string,
interval: KlineInterval,
forceNewConnection?: boolean
): WebSocket;Subscribe to index klines(candles) for a symbol in a coinm futures
Parameters
| Parameter | Type |
|---|---|
symbol | string |
interval | KlineInterval |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeIsolatedMarginUserDataStream()
subscribeIsolatedMarginUserDataStream(
symbol: string,
forceNewConnection?: boolean,
isReconnecting?: boolean
): Promise<WebSocket | undefined>;Subscribe to isolated margin user data stream - listen key is automatically generated.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
Promise<WebSocket | undefined>
subscribeKlines()
subscribeKlines(
symbol: string,
interval: KlineInterval,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to klines(candles) for a symbol in a market category
Parameters
| Parameter | Type |
|---|---|
symbol | string |
interval | KlineInterval |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeMarginUserDataStream()
subscribeMarginUserDataStream(forceNewConnection?: boolean, isReconnecting?: boolean): Promise<WebSocket | undefined>;Subscribe to margin user data stream - listen key is automatically generated.
Parameters
| Parameter | Type |
|---|---|
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
Promise<WebSocket | undefined>
subscribeMarkPrice()
subscribeMarkPrice(
symbol: string,
market: "usdm" | "coinm",
updateSpeedMs?: 1000 | 3000,
forceNewConnection?: boolean
): WebSocket;Subscribe to mark price for a symbol in a market category
Parameters
| Parameter | Type | Default value |
|---|---|---|
symbol | string | undefined |
market | "usdm" | "coinm" | undefined |
updateSpeedMs? | 1000 | 3000 | 3000 |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeMarkPriceKlines()
subscribeMarkPriceKlines(
symbol: string,
interval: KlineInterval,
forceNewConnection?: boolean
): WebSocket;Subscribe to index klines(candles) for a symbol in a coinm futures
Parameters
| Parameter | Type |
|---|---|
symbol | string |
interval | KlineInterval |
forceNewConnection? | boolean |
Returns
WebSocket
subscribePartialBookDepths()
subscribePartialBookDepths(
symbol: string,
levels: 5 | 10 | 20,
updateMs: 100 | 250 | 500 | 1000,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to partial book depths (snapshots).
Note:
- spot only supports 1000ms or 100ms for updateMs
- futures only support 100, 250 or 500ms for updateMs
Use getContextFromWsKey(data.wsKey) to extract symbol from events
Parameters
| Parameter | Type |
|---|---|
symbol | string |
levels | 5 | 10 | 20 |
updateMs | 100 | 250 | 500 | 1000 |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotAggregateTrades()
subscribeSpotAggregateTrades(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to aggregate trades for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotAll24hrTickers()
subscribeSpotAll24hrTickers(forceNewConnection?: boolean): WebSocket;Subscribe to 24hr ticker in spot markets.
Parameters
| Parameter | Type |
|---|---|
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotAllBookTickers()
subscribeSpotAllBookTickers(forceNewConnection?: boolean): WebSocket;Subscribe to best bid/ask for all symbols in spot markets.
Parameters
| Parameter | Type |
|---|---|
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotAllMini24hrTickers()
subscribeSpotAllMini24hrTickers(forceNewConnection?: boolean): WebSocket;Subscribe to mini 24hr mini ticker in spot markets.
Parameters
| Parameter | Type |
|---|---|
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotBlockTrades()
subscribeSpotBlockTrades(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to block trades for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotDiffBookDepth()
subscribeSpotDiffBookDepth(
symbol: string,
updateMs?: 100 | 1000,
forceNewConnection?: boolean
): WebSocket;Subscribe to spot orderbook depth updates to locally manage an order book.
Parameters
| Parameter | Type | Default value |
|---|---|---|
symbol | string | undefined |
updateMs? | 100 | 1000 | 1000 |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeSpotKline()
subscribeSpotKline(
symbol: string,
interval: KlineInterval,
forceNewConnection?: boolean
): WebSocket;Subscribe to candles for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
interval | KlineInterval |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotPartialBookDepth()
subscribeSpotPartialBookDepth(
symbol: string,
levels: 5 | 10 | 20,
updateMs?: 100 | 1000,
forceNewConnection?: boolean
): WebSocket;Subscribe to top bid/ask levels for symbol in spot markets.
Parameters
| Parameter | Type | Default value |
|---|---|---|
symbol | string | undefined |
levels | 5 | 10 | 20 | undefined |
updateMs? | 100 | 1000 | 1000 |
forceNewConnection? | boolean | undefined |
Returns
WebSocket
subscribeSpotSymbol24hrTicker()
subscribeSpotSymbol24hrTicker(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to 24hr ticker for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotSymbolBookTicker()
subscribeSpotSymbolBookTicker(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to best bid/ask for symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotSymbolMini24hrTicker()
subscribeSpotSymbolMini24hrTicker(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to mini 24hr ticker for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotTrades()
subscribeSpotTrades(symbol: string, forceNewConnection?: boolean): WebSocket;Subscribe to trades for a symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSpotUserDataStream()
subscribeSpotUserDataStream(forceNewConnection?: boolean, isReconnecting?: boolean): Promise<WebSocket | undefined>;Subscribe to spot user data stream - listen key is automaticallyr generated. Calling multiple times only opens one connection.
Parameters
| Parameter | Type |
|---|---|
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
Promise<WebSocket | undefined>
subscribeSpotUserDataStreamWithListenKey()
subscribeSpotUserDataStreamWithListenKey(
listenKey: string,
forceNewConnection?: boolean,
isReconnecting?: boolean
): WebSocket | undefined;Subscribe to a spot user data stream. Use REST client to generate and persist listen key. Supports spot, margin & isolated margin listen keys.
Parameters
| Parameter | Type |
|---|---|
listenKey | string |
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
WebSocket | undefined
subscribeSymbol24hrTicker()
subscribeSymbol24hrTicker(
symbol: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to 24hr ticker for a symbol in any market.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSymbolBookTicker()
subscribeSymbolBookTicker(
symbol: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to best bid/ask for symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSymbolLiquidationOrders()
subscribeSymbolLiquidationOrders(
symbol: string,
market: "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to best bid/ask for symbol in spot markets.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeSymbolMini24hrTicker()
subscribeSymbolMini24hrTicker(
symbol: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to mini 24hr ticker for a symbol in market category.
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeTrades()
subscribeTrades(
symbol: string,
market: "spot" | "usdm" | "coinm",
forceNewConnection?: boolean
): WebSocket;Subscribe to trades for a symbol in a market category IMPORTANT: This topic for usdm and coinm is not listed in the api docs and might stop working without warning
Parameters
| Parameter | Type |
|---|---|
symbol | string |
market | "spot" | "usdm" | "coinm" |
forceNewConnection? | boolean |
Returns
WebSocket
subscribeUsdFuturesUserDataStream()
subscribeUsdFuturesUserDataStream(
isTestnet?: boolean,
forceNewConnection?: boolean,
isReconnecting?: boolean
): Promise<WebSocket | undefined>;Subscribe to USD-M Futures user data stream - listen key is automatically generated.
Parameters
| Parameter | Type |
|---|---|
isTestnet? | boolean |
forceNewConnection? | boolean |
isReconnecting? | boolean |
Returns
Promise<WebSocket | undefined>
tryWsPing()
tryWsPing(wsKey: string): void;Parameters
| Parameter | Type |
|---|---|
wsKey | string |
Returns
void
tryWsSend()
tryWsSend(wsKey: string, wsMessage: string): void;Parameters
| Parameter | Type |
|---|---|
wsKey | string |
wsMessage | string |
Returns
void