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?): WebSocketObservable
Parameters
Parameter | Type |
---|---|
url | string | URL |
protocols ? | string | string [] | WebSocketInit |
Returns
WebSocketObservable
Inherited from
WebSocket.constructor
Properties
binaryType
binaryType: BinaryType;
Inherited from
WebSocket.binaryType
bufferedAmount
readonly bufferedAmount: number;
Inherited from
WebSocket.bufferedAmount
CLOSED
readonly CLOSED: number;
Inherited from
WebSocket.CLOSED
CLOSING
readonly CLOSING: number;
Inherited from
WebSocket.CLOSING
CONNECTING
readonly CONNECTING: number;
Inherited from
WebSocket.CONNECTING
extensions
readonly extensions: string;
Inherited from
WebSocket.extensions
onclose
onclose: null | (this, ev) => any;
Inherited from
WebSocket.onclose
onerror
onerror: null | (this, ev) => any;
Inherited from
WebSocket.onerror
onmessage
onmessage: null | (this, ev) => any;
Inherited from
WebSocket.onmessage
onopen
onopen: null | (this, ev) => any;
Inherited from
WebSocket.onopen
OPEN
readonly OPEN: number;
Inherited from
WebSocket.OPEN
protocol
readonly protocol: string;
Inherited from
WebSocket.protocol
readyState
readonly readyState: number;
Inherited from
WebSocket.readyState
url
readonly url: string;
Inherited from
WebSocket.url
CLOSED
readonly static CLOSED: number;
Inherited from
WebSocket.CLOSED
CLOSING
readonly static CLOSING: number;
Inherited from
WebSocket.CLOSING
CONNECTING
readonly static CONNECTING: number;
Inherited from
WebSocket.CONNECTING
OPEN
readonly static OPEN: number;
Inherited from
WebSocket.OPEN
Methods
addEventListener()
Call Signature
addEventListener<K>(
type,
listener,
options?): void
Adds 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.addEventListener
Call Signature
addEventListener(
type,
listener,
options?): void
Adds 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.addEventListener
asObservable()
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?): void
Parameters
Parameter | Type |
---|---|
code ? | number |
reason ? | string |
Returns
void
Inherited from
WebSocket.close
dispatchEvent()
dispatchEvent(event): boolean
Dispatches 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.dispatchEvent
removeEventListener()
Call Signature
removeEventListener<K>(
type,
listener,
options?): void
Removes 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.removeEventListener
Call Signature
removeEventListener(
type,
listener,
options?): void
Removes 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.removeEventListener
send()
send(data): void
Parameters
Parameter | Type |
---|---|
data | string | ArrayBufferLike | Blob | ArrayBufferView <ArrayBufferLike > |
Returns
void
Inherited from
WebSocket.send