A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.

Constructors

  • Creates a new websocket.

    Parameters

    • url: string

      to connect to.

    • Optionalprotocols: string | string[]

      optional protocols to use.

    • Optionaloptions: WebsocketOptions

      optional options to use.

    Returns Websocket

Accessors

  • get backoff(): undefined | Backoff

    Getter for the backoff.

    Returns undefined | Backoff

    the backoff, or undefined if none was provided.

  • get buffer(): | undefined
    | WebsocketBuffer<
        string
        | ArrayBufferLike
        | ArrayBufferView<ArrayBufferLike>
        | Blob,
    >

    Getter for the buffer.

    Returns
        | undefined
        | WebsocketBuffer<
            string
            | ArrayBufferLike
            | ArrayBufferView<ArrayBufferLike>
            | Blob,
        >

    the buffer, or undefined if none was provided.

  • get closedByUser(): boolean

    Whether the websocket was closed by the user. A websocket is closed by the user by calling close().

    Returns boolean

    true if the websocket was closed by the user, false otherwise.

  • get instantReconnect(): undefined | boolean

    Getter for the instantReconnect.

    Returns undefined | boolean

    the instantReconnect, or undefined if none was provided.

  • get lastConnection(): undefined | Date

    Getter for the last 'open' event, e.g. the last time the websocket was connected.

    Returns undefined | Date

    the last 'open' event, or undefined if the websocket was never connected.

  • get maxRetries(): undefined | number

    Getter for the maxRetries.

    Returns undefined | number

    the maxRetries, or undefined if none was provided (no limit).

  • get protocols(): undefined | string | string[]

    Getter for the protocols.

    Returns undefined | string | string[]

    the protocols, or undefined if none were provided.

  • get url(): string

    Getter for the url.

    Returns string

    the url.

Methods

  • Sends data over the websocket.

    If the websocket is not connected and a buffer was provided on creation, the data will be added to the buffer. If no buffer was provided or the websocket was closed by the user, the data will be dropped.

    Parameters

    • data: string | ArrayBufferLike | ArrayBufferView<ArrayBufferLike> | Blob

      to send.

    Returns void