websocket-ts
    Preparing search index...

    Class Websocket

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

    Index
    • Creates a new websocket.

      Parameters

      • url: UrlProvider

        to connect to, or a function that returns a URL.

      • Optionalprotocols: string | string[]

        optional protocols to use.

      • Optionaloptions: WebsocketOptions

        optional options to use.

      Returns Websocket

      Error if retry options (maxRetries, instantReconnect) are set without a backoff.

      Error if maxRetries is neither Infinity nor a non-negative integer.

    • get backoff(): Backoff | undefined

      Getter for the backoff.

      Returns Backoff | undefined

      the backoff, or undefined if none was provided.

    • get binaryType(): BinaryType

      Getter for the binaryType of the underlying websocket.

      Returns BinaryType

      the binaryType of the underlying websocket.

    • set binaryType(value: BinaryType): void

      Setter for the binaryType of the underlying websocket. The value is remembered and re-applied whenever a new underlying websocket is created after a reconnect.

      Parameters

      • value: BinaryType

        to set, 'blob' or 'arraybuffer'.

      Returns void

    • get buffer(): WebsocketBuffer<string | Blob | BufferSource> | undefined

      Getter for the buffer.

      Returns WebsocketBuffer<string | Blob | BufferSource> | undefined

      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(): boolean | undefined

      Getter for the instantReconnect.

      Returns boolean | undefined

      the instantReconnect, or undefined if none was provided.

    • get lastConnection(): Date | undefined

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

      Returns Date | undefined

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

    • get maxRetries(): number | undefined

      Getter for the maxRetries.

      Returns number | undefined

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

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

      Getter for the protocols.

      Returns string | string[] | undefined

      the protocols, or undefined if none were provided.

    • get url(): string

      Getter for the url of the most recently constructed underlying websocket. This is not necessarily the URL of the last successful connection, and a URL that the WebSocket constructor rejected, or whose connection attempt was superseded, is not recorded.

      Returns string

      the url.

    • (Re-)connects the websocket immediately, regardless of its current state: any scheduled retry is cancelled, the backoff and its retry-counter are reset, the URL provider is resolved again and a new connection is opened. This also revives a websocket that was closed by the user or that gave up after exceeding maxRetries (see the 'exhausted' event).

      Typical uses: a "reconnect" button after the 'exhausted' event, reacting to the browser's 'online' event, or forcing the URL provider to pick up a new auth token without waiting for the connection to drop.

      Returns void

      whatever the URL provider or the WebSocket constructor throws.

    • 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 | Blob | BufferSource

        to send.

      Returns void