Skip to content

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 ​

ts
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 ​

MDN Reference

Extends ​

  • WebSocket

Constructors ​

Constructor ​

ts
new WebSocket(url, protocols?): WebSocketObservable

Parameters ​

ParameterType
urlstring | URL
protocols?string | string[] | WebSocketInit

Returns ​

WebSocketObservable

Inherited from ​

ts
WebSocket.constructor

Properties ​

binaryType ​

ts
binaryType: BinaryType;

Inherited from ​

ts
WebSocket.binaryType

bufferedAmount ​

ts
readonly bufferedAmount: number;

Inherited from ​

ts
WebSocket.bufferedAmount

CLOSED ​

ts
readonly CLOSED: number;

Inherited from ​

ts
WebSocket.CLOSED

CLOSING ​

ts
readonly CLOSING: number;

Inherited from ​

ts
WebSocket.CLOSING

CONNECTING ​

ts
readonly CONNECTING: number;

Inherited from ​

ts
WebSocket.CONNECTING

extensions ​

ts
readonly extensions: string;

Inherited from ​

ts
WebSocket.extensions

onclose ​

ts
onclose: null | (this, ev) => any;

Inherited from ​

ts
WebSocket.onclose

onerror ​

ts
onerror: null | (this, ev) => any;

Inherited from ​

ts
WebSocket.onerror

onmessage ​

ts
onmessage: null | (this, ev) => any;

Inherited from ​

ts
WebSocket.onmessage

onopen ​

ts
onopen: null | (this, ev) => any;

Inherited from ​

ts
WebSocket.onopen

OPEN ​

ts
readonly OPEN: number;

Inherited from ​

ts
WebSocket.OPEN

protocol ​

ts
readonly protocol: string;

Inherited from ​

ts
WebSocket.protocol

readyState ​

ts
readonly readyState: number;

Inherited from ​

ts
WebSocket.readyState

url ​

ts
readonly url: string;

Inherited from ​

ts
WebSocket.url

CLOSED ​

ts
readonly static CLOSED: number;

Inherited from ​

ts
WebSocket.CLOSED

CLOSING ​

ts
readonly static CLOSING: number;

Inherited from ​

ts
WebSocket.CLOSING

CONNECTING ​

ts
readonly static CONNECTING: number;

Inherited from ​

ts
WebSocket.CONNECTING

OPEN ​

ts
readonly static OPEN: number;

Inherited from ​

ts
WebSocket.OPEN

Methods ​

addEventListener() ​

Call Signature ​

ts
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 ​
ParameterType
typeK
listener(this, ev) => any
options?boolean | AddEventListenerOptions
Returns ​

void

Inherited from ​
ts
WebSocket.addEventListener

Call Signature ​

ts
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 ​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | AddEventListenerOptions
Returns ​

void

Inherited from ​
ts
WebSocket.addEventListener

asObservable() ​

Call Signature ​

ts
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 ParameterDescription
TThe type of data contained in message events
Parameters ​
ParameterTypeDescription
opts{ dematerialize: true; }Configuration options for the observable
opts.dematerializetrueWhen 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 ​

ts
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 ParameterDescription
TThe type of data contained in message events
Parameters ​
ParameterTypeDescription
opts?{ dematerialize: false; }Configuration options for the observable
opts.dematerialize?falseWhen 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() ​

ts
close(code?, reason?): void

Parameters ​

ParameterType
code?number
reason?string

Returns ​

void

Inherited from ​

ts
WebSocket.close

dispatchEvent() ​

ts
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 ​

ParameterType
eventEvent

Returns ​

boolean

Inherited from ​

ts
WebSocket.dispatchEvent

removeEventListener() ​

Call Signature ​

ts
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 ​
ParameterType
typeK
listener(this, ev) => any
options?boolean | EventListenerOptions
Returns ​

void

Inherited from ​
ts
WebSocket.removeEventListener

Call Signature ​

ts
removeEventListener(
   type, 
   listener, 
   options?): void

Removes the event listener in target's event listener list with the same type, callback, and options.

Parameters ​
ParameterType
typestring
listenerEventListenerOrEventListenerObject
options?boolean | EventListenerOptions
Returns ​

void

Inherited from ​
ts
WebSocket.removeEventListener

send() ​

ts
send(data): void

Parameters ​

ParameterType
datastring | ArrayBufferLike | Blob | ArrayBufferView<ArrayBufferLike>

Returns ​

void

Inherited from ​

ts
WebSocket.send

Released under the MIT License.