Binance Connector JS
    Preparing search index...

    Class WebsocketStreamsBase

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    connectionPool: WebsocketConnection[]
    logger: Logger = ...
    streamCallbackMap: Map<string, Set<(data: unknown) => void>> = ...
    wsURL: string

    Methods

    • Cleans up WebSocket connection resources. Removes all listeners and clears any associated timers for the provided WebSocket client.

      Parameters

      • ws: WebSocket

        The WebSocket client to clean up.

      Returns void

      void

    • Connects to the WebSocket server and subscribes to the specified streams. This method returns a Promise that resolves when the connection is established, or rejects with an error if the connection fails to be established within 10 seconds.

      Parameters

      • stream: string | string[] = []

        A single stream name or an array of stream names to subscribe to.

      Returns Promise<void>

      A Promise that resolves when the connection is established.

    • Connects all WebSocket connections in the pool

      Parameters

      • url: string

        The Websocket server URL.

      Returns Promise<void>

      A promise that resolves when all connections are established.

    • Disconnects the WebSocket connection and clears the stream callback map. This method is called to clean up the connection and associated resources.

      Returns Promise<void>

    • Parameters

      • event: "open" | "message" | "error" | "close" | "ping" | "pong"
      • ...args: any[]

      Returns void

    • Retrieves available WebSocket connections based on the connection mode and readiness. In 'single' mode, returns the first connection in the pool. In 'pool' mode, filters and returns connections that are ready for use.

      Parameters

      • allowNonEstablishedWebsockets: boolean = false

        Optional flag to include non-established WebSocket connections.

      Returns WebsocketConnection[]

      An array of available WebSocket connections.

    • Gets a WebSocket connection from the pool or single connection. If the connection mode is 'single', it returns the first connection in the pool. If the connection mode is 'pool', it returns an available connection from the pool, using a round-robin selection strategy. If no available connections are found, it throws an error.

      Parameters

      • allowNonEstablishedWebsockets: boolean = false

        A boolean indicating whether to allow connections that are not established.

      Returns WebsocketConnection

      The selected WebSocket connection.

    • Checks if the provided WebSocket connection is ready for use. A connection is considered ready if it is open, has no pending reconnection, and has not been closed.

      Parameters

      • connection: WebsocketConnection

        The WebSocket connection to check.

      • allowNonEstablishedWebsockets: boolean = false

        An optional flag to allow non-established WebSocket connections.

      Returns boolean

      true if the connection is ready, false otherwise.

    • Checks if the specified stream is currently subscribed.

      Parameters

      • stream: string

        The name of the stream to check.

      Returns boolean

      true if the stream is currently subscribed, false otherwise.

    • Parameters

      • event: "open" | "message" | "error" | "close" | "ping" | "pong"
      • listener: (...args: any[]) => void

      Returns void

    • Parameters

      • event: "open" | "message" | "error" | "close" | "ping" | "pong"
      • listener: (...args: any[]) => void

      Returns void

    • Handles incoming WebSocket messages, parsing the data and invoking the appropriate callback function. If the message contains a stream name that is registered in the streamCallbackMap, the corresponding callback function is called with the message data. If the message cannot be parsed, an error is logged.

      Parameters

      • data: string

        The raw WebSocket message data.

      • connection: WebsocketConnection

        The WebSocket connection that received the message.

      Returns void

    • Called when the WebSocket connection is opened. Processes any pending subscriptions for the target connection.

      Parameters

      • url: string

        The URL of the WebSocket connection.

      • targetConnection: WebsocketConnection

        The WebSocket connection that was opened.

      • oldWSConnection: WebSocket

      Returns void

    • Sends a ping message to all connected Websocket servers in the pool. If no connections are ready, a warning is logged. For each active connection, the ping message is sent, and debug logs provide details.

      Returns void

      Error if a Websocket client is not set for a connection.

    • Sends a payload through the WebSocket connection.

      Type Parameters

      • T = unknown

      Parameters

      • payload: string

        Message to send.

      • Optionalid: string

        Optional request identifier.

      • promiseBased: boolean = true

        Whether to return a promise.

      • timeout: number = 5000

        Timeout duration in milliseconds.

      • Optionalconnection: WebsocketConnection

        The WebSocket connection to use.

      Returns void | Promise<WebsocketApiResponse<T>>

      A promise if promiseBased is true, void otherwise.

      Error if not connected or WebSocket client is not set.

    • Subscribes to one or multiple WebSocket streams Handles both single and pool modes

      Parameters

      • stream: string | string[]

        Single stream name or array of stream names to subscribe to

      • Optionalid: string

        Optional subscription ID

      Returns void

      void

    • Unsubscribes from one or multiple WebSocket streams Handles both single and pool modes

      Parameters

      • stream: string | string[]

        Single stream name or array of stream names to unsubscribe from

      • Optionalid: string

        Optional unsubscription ID

      Returns void

      void