Documentation / @eventkit/http / WebSocket
Class: WebSocket
A drop-in replacement for the standard WebSocket class that provides an Observable interface for handling WebSocket connections. This class extends the native WebSocket class and adds the ability to consume events as an AsyncObservable using the asObservable method.
Example
import { WebSocket } from "@eventkit/http";
const ws = new WebSocket("wss://api.example.com/ws");
// Get raw message data
ws.asObservable<string>().subscribe(data => {
console.log("Received:", data);
});
// Get full event objects including metadata
ws.asObservable<string>({ dematerialize: true }).subscribe(event => {
switch (event.type) {
case "message":
console.log("Message:", event.data.data);
break;
case "error":
console.error("Error:", event.data);
break;
case "close":
console.log("Connection closed:", event.data.code, event.data.reason);
break;
case "open":
console.log("Connection opened");
break;
}
});See
Extends
WebSocket
Constructors
Constructor
new WebSocket(url, protocols?): WebSocketObservableParameters
| Parameter | Type |
|---|---|
url | string | URL |
protocols? | string | string[] | WebSocketInit |
Returns
WebSocketObservable
Inherited from
WebSocket.constructorProperties
binaryType
binaryType: BinaryType;Inherited from
WebSocket.binaryTypebufferedAmount
readonly bufferedAmount: number;Inherited from
WebSocket.bufferedAmountCLOSED
readonly CLOSED: number;Inherited from
WebSocket.CLOSEDCLOSING
readonly CLOSING: number;Inherited from
WebSocket.CLOSINGCONNECTING
readonly CONNECTING: number;Inherited from
WebSocket.CONNECTINGextensions
readonly extensions: string;Inherited from
WebSocket.extensionsonclose
onclose: null | (this, ev) => any;Inherited from
WebSocket.oncloseonerror
onerror: null | (this, ev) => any;Inherited from
WebSocket.onerroronmessage
onmessage: null | (this, ev) => any;Inherited from
WebSocket.onmessageonopen
onopen: null | (this, ev) => any;Inherited from
WebSocket.onopenOPEN
readonly OPEN: number;Inherited from
WebSocket.OPENprotocol
readonly protocol: string;Inherited from
WebSocket.protocolreadyState
readonly readyState: number;Inherited from
WebSocket.readyStateurl
readonly url: string;Inherited from
WebSocket.urlCLOSED
readonly static CLOSED: number;Inherited from
WebSocket.CLOSEDCLOSING
readonly static CLOSING: number;Inherited from
WebSocket.CLOSINGCONNECTING
readonly static CONNECTING: number;Inherited from
WebSocket.CONNECTINGOPEN
readonly static OPEN: number;Inherited from
WebSocket.OPENMethods
addEventListener()
Call Signature
addEventListener<K>(
type,
listener,
options?): voidAdds a new handler for the type event. Any given listener is added only once per type and per capture option value.
If the once option is true, the listener is removed after the next time a type event is dispatched.
The capture option is not used by Node.js in any functional way other than tracking registered event listeners per the EventTarget specification. Specifically, the capture option is used as part of the key when registering a listener. Any individual listener may be added once with capture = false, and once with capture = true.
Type Parameters
| Type Parameter |
|---|
K extends keyof WebSocketEventMap |
Parameters
| Parameter | Type |
|---|---|
type | K |
listener | (this, ev) => any |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
WebSocket.addEventListenerCall Signature
addEventListener(
type,
listener,
options?): voidAdds a new handler for the type event. Any given listener is added only once per type and per capture option value.
If the once option is true, the listener is removed after the next time a type event is dispatched.
The capture option is not used by Node.js in any functional way other than tracking registered event listeners per the EventTarget specification. Specifically, the capture option is used as part of the key when registering a listener. Any individual listener may be added once with capture = false, and once with capture = true.
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | EventListenerOrEventListenerObject |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
WebSocket.addEventListenerasObservable()
Call Signature
asObservable<T>(opts): AsyncObservable<WebSocketEvent<T>>Converts the WebSocket into an AsyncObservable that yields either raw message data or full event objects depending on the options provided.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of data contained in message events |
Parameters
| Parameter | Type | Description |
|---|---|---|
opts | { dematerialize: true; } | Configuration options for the observable |
opts.dematerialize | true | When true, yields full event objects including metadata. When false or omitted, yields only the message data. |
Returns
AsyncObservable<WebSocketEvent<T>>
An AsyncObservable that yields either raw message data or full event objects
Call Signature
asObservable<T>(opts?): AsyncObservable<T>Converts the WebSocket into an AsyncObservable that yields either raw message data or full event objects depending on the options provided.
Type Parameters
| Type Parameter | Description |
|---|---|
T | The type of data contained in message events |
Parameters
| Parameter | Type | Description |
|---|---|---|
opts? | { dematerialize: false; } | Configuration options for the observable |
opts.dematerialize? | false | When true, yields full event objects including metadata. When false or omitted, yields only the message data. |
Returns
AsyncObservable<T>
An AsyncObservable that yields either raw message data or full event objects
close()
close(code?, reason?): voidParameters
| Parameter | Type |
|---|---|
code? | number |
reason? | string |
Returns
void
Inherited from
WebSocket.closedispatchEvent()
dispatchEvent(event): booleanDispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.
Parameters
| Parameter | Type |
|---|---|
event | Event |
Returns
boolean
Inherited from
WebSocket.dispatchEventremoveEventListener()
Call Signature
removeEventListener<K>(
type,
listener,
options?): voidRemoves the event listener in target's event listener list with the same type, callback, and options.
Type Parameters
| Type Parameter |
|---|
K extends keyof WebSocketEventMap |
Parameters
| Parameter | Type |
|---|---|
type | K |
listener | (this, ev) => any |
options? | boolean | EventListenerOptions |
Returns
void
Inherited from
WebSocket.removeEventListenerCall Signature
removeEventListener(
type,
listener,
options?): voidRemoves the event listener in target's event listener list with the same type, callback, and options.
Parameters
| Parameter | Type |
|---|---|
type | string |
listener | EventListenerOrEventListenerObject |
options? | boolean | EventListenerOptions |
Returns
void
Inherited from
WebSocket.removeEventListenersend()
send(data): voidParameters
| Parameter | Type |
|---|---|
data | string | ArrayBufferLike | Blob | ArrayBufferView<ArrayBufferLike> |
Returns
void
Inherited from
WebSocket.send