Binance Connector JS
    Preparing search index...

    Class WebsocketAPIBase

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

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

    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

    • Establishes a WebSocket connection to Binance

      Returns Promise<void>

      Promise that resolves when connection is established

      Error if connection times out

    • 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 from the WebSocket server. If there is no active connection, a warning is logged. Otherwise, all connections in the connection pool are closed gracefully, and a message is logged indicating that the connection has been disconnected.

      Returns Promise<void>

      A Promise that resolves when all connections have been closed.

      Error if the WebSocket client is not set.

    • 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.

    • Returns the URL to use when reconnecting. Derived classes should override this to provide dynamic URLs.

      Parameters

      • defaultURL: string

        The URL originally passed during the first connection.

      • targetConnection: WebsocketConnection

        The WebSocket connection being connected.

      Returns string

      The URL to reconnect to.

    • 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.

    • 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 the opening of a WebSocket connection.

      Parameters

      • url: string

        The URL of the WebSocket server.

      • targetConnection: WebsocketConnection

        The WebSocket connection being opened.

      • oldWSConnection: WebSocket

        The WebSocket client instance associated with the old connection.

      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.