Creates a new websocket.
to connect to, or a function that returns a URL.
Optionalprotocols: string | string[]
optional protocols to use.
Optionaloptions: WebsocketOptions
optional options to use.
Getter for the binaryType of the underlying websocket.
the binaryType of the underlying websocket.
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.
to set, 'blob' or 'arraybuffer'.
Getter for the buffer.
the buffer, or undefined if none was provided.
Getter for the bufferedAmount of the underlying websocket.
the bufferedAmount of the underlying websocket.
Whether the websocket was closed by the user. A websocket is closed by the user by calling close().
true if the websocket was closed by the user, false otherwise.
Getter for the extensions of the underlying websocket.
the extensions of the underlying websocket.
Getter for the instantReconnect.
the instantReconnect, or undefined if none was provided.
Getter for the time of the last 'open' event, i.e. the last time the websocket was connected.
the time of the last 'open' event, or undefined if the websocket was never connected.
Getter for the maxRetries.
the maxRetries, or undefined if none was provided (no limit).
Getter for the protocols.
the protocols, or undefined if none were provided.
Getter for the readyState of the underlying websocket.
the readyState of the underlying websocket.
Getter for the underlying websocket. This can be used to access the browser's native websocket directly.
the underlying websocket.
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.
the url.
Adds an event listener for the given event-type. When an AbortSignal is provided in the options, aborting it removes exactly this registration; a listener whose signal is already aborted is never registered, mirroring the native EventTarget.
of the event to add the listener for.
to add.
Optionaloptions: WebsocketEventListenerOptions
to use when adding the listener.
Close the websocket. No automatic connection-retry will be attempted after this, until reconnect() is called.
Optionalcode: number
optional close code.
Optionalreason: string
optional close reason.
(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.
Removes all event listeners for the given event-type that match the given listener.
Matching is done by listener identity only, mirroring the native EventTarget: the options a listener was added with are ignored when matching.
of the event to remove the listener for.
to remove.
Optionaloptions: WebsocketEventListenerOptions
ignored when matching, present for backward compatibility.
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.
to send.
A websocket wrapper that can be configured to reconnect automatically and buffer messages when the websocket is not connected.