WebSocketSession
Typed WebSocket session: send and receive messages, observe state, and close.
@defjs/core · Type
Signature
interface WebSocketSession< TIncoming = unknown, TOutgoing = never, > extends AsyncDisposable { readonly bufferedAmount: number; readonly connection: WebSocketConnectionInfo; readonly closed: Promise<WebSocketCloseInfo>; readonly receive: AsyncIterable<TIncoming>; readonly state: WebSocketState; close(code?: number, reason?: string): void; [Symbol.asyncDispose](): PromiseLike<void>; onRuntimeError(listener: (error: unknown) => void): () => void; onStateChange(listener: (state: WebSocketState) => void): () => void; send(message: TOutgoing): void; }
-
stateis the logical WebSocketState;connectionis the latest WebSocketConnectionInfo.bufferedAmountis the native unsent byte count, or0with no physical socket. -
receiveyields incoming messages already parsed with theincomingstruct. It is one-consumer: asking for a second iterator throws aTypeError. A frame that cannot be parsed is reported toonRuntimeErrorlisteners and skipped. -
send(message)validates and encodes the message with theoutgoingstruct, then sends JSON text. It sends immediately whileopen, queues whilereconnectingwhenmaxOutgoingQueueSizeallows, and throws aDOMExceptionnamedInvalidStateErrorwhen the session is not writable. It also throws when nooutgoingstruct is declared, the message fails the struct, or the queue is disabled or full. -
After encoding, a value JSON cannot faithfully represent throws
TypeErrorbefore sending or entering the queue. This includes non-finite numbers, functions, symbols,Map,Set, streams, and class instances without a JSON representation, at the root or nested. The same guard protects heartbeat messages; a heartbeat serialization failure ends the session. Struct validation may reject a value first. -
close(code?, reason?)requests closure and stops the heartbeat.codemust be1000or in3000..4999, andreasonat most 123 UTF-8 bytes; invalid arguments throw before the state changes. -
closedresolves with the logical terminal WebSocketCloseInfo. -
onStateChangeandonRuntimeErrorreturn an unsubscribe function.
Prefer await using ownedSession = session for owned scope cleanup. Manual session.close() and await session.closed remain available.
async function consume(
session: WebSocketSession<unknown, never>,
): Promise<void> {
await using ownedSession = session;
for await (const message of ownedSession.receive) console.log(message);
}The async disposer requests native close and waits for Defjs-owned teardown, but it bounds a missing close event at one second and may reject with a DOMException named TimeoutError. It cannot prove that the physical TCP connection closed.
Members
Prop
Type
bufferedAmount numberconnection WebSocketConnectionInfoclosed Promise<WebSocketCloseInfo>receive AsyncIterable<TIncoming>state WebSocketStateclose (code?: number, reason?: string) => void[Symbol.asyncDispose] () => PromiseLike<void>onRuntimeError (listener: (error: unknown) => void) => () => voidonStateChange (listener: (state: WebSocketState) => void) => () => voidsend (message: TOutgoing) => void