Skip to content

WebSocketPeer

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:100

One end of a WebSocket connection, over any Transport: after an HTTP/1.1 Upgrade, or — the same way — over an HTTP/2 or HTTP/3 stream.

Messages are sent as single frames and received whole: fragments are reassembled, up to maxMessageSize, and text is checked to be UTF-8. Pings are answered automatically. Every protocol violation fails the connection with the close code RFC 6455 assigns to it.

Closing follows §7: close sends a close frame and waits for the peer’s, a close frame received is echoed, and the server end closes the transport first. Either way close is emitted once.

acceptWebSocket and connectWebSocket for the opening handshake.

new WebSocketPeer(options): WebSocketPeer

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:116

WebSocketPeerOptions

WebSocketPeer

EventDispatcherMapBase<WebSocketPeerEvents>.constructor

readonly protocol: string | null

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:102

The subprotocol the handshake settled on, or null.

get state(): WebSocketState

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:136

WebSocketState

[asyncDispose](): Promise<void>

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:203

Closes the peer, so it can be used with await using.

Promise<void>


close(code?, reason?): Promise<void>

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:183

Start the closing handshake and wait for it to complete, or for closeTimeoutMs to pass, whichever comes first.

number = 1000

A sendable close code; defaults to 1000.

string = ""

At most 123 UTF-8 bytes.

Promise<void>

When code may not be sent or reason is too long.


destroy(error?): void

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:197

Drop the connection without a closing handshake.

unknown

void


isSubscribed<K>(event, listener): boolean

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:38

Check if a callback is subscribed to a specific event.

K extends keyof WebSocketPeerEvents

K

The event name.

Callable<WebSocketPeerEvents[K]>

The callback to check.

boolean

EventDispatcherMapBase.isSubscribed


ping(payload?, signal?): Promise<void>

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:165

Send a ping. The peer answers with a pong carrying the same payload.

Uint8Array = ...

At most 125 bytes.

AbortSignal

Promise<void>

When the peer is closing or closed.

When payload exceeds 125 bytes.


send(data, signal?): Promise<void>

Defined in: packages/net-websocket/src/peer/websocket-peer.ts:146

Send one message, as a single frame: a string as text, bytes as binary.

string | Uint8Array<ArrayBufferLike>

AbortSignal

Cancels the wait for transport capacity.

Promise<void>

When the peer is closing or closed.


subscribe<K>(event, listener, options?): Callable | null

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:23

K extends keyof WebSocketPeerEvents

K

Callable<WebSocketPeerEvents[K]>

EventDispatcherSubscribeOptions

Callable | null

EventDispatcherMapBase.subscribe


unsubscribe<K>(event, listener): void

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:31

Unsubscribe a callback from a specific event.

K extends keyof WebSocketPeerEvents

K

The event name.

Callable<WebSocketPeerEvents[K]>

The callback to remove.

void

EventDispatcherMapBase.unsubscribe


wait<K>(event, options?): Promise<WebSocketPeerEvents[K]>

Defined in: packages/async/src/events/event-dispatcher-map-base.ts:45

Wait for the next occurrence of a specific event, optionally matching a predicate.

This is a convenience method that subscribes to the dispatcher, waits for the next event that matches the predicate (if provided), and then unsubscribes automatically.

K extends keyof WebSocketPeerEvents

K

The event name to wait for.

EventDispatcherWaitOptions<WebSocketPeerEvents[K]>

Optional settings including predicate and abort signal.

Promise<WebSocketPeerEvents[K]>

A promise that resolves with the event arguments as an array.

EventDispatcherMapBase.wait