Skip to content

FlareWebSocketMessage

class FlareWebSocketMessage

Runtime-agnostic representation of a single WebSocket message.

Wraps the raw decoded payload (text as string, binary as Uint8Array) with lazy, memoized accessors, mirroring FlareRequest: allocating a FlareWebSocketMessage costs only the wrapper, and decoding/parsing is deferred to first access and cached, so nothing is spent on the per-message hot path unless a handler actually reads it. The same type represents a message in either direction - the one delivered to a message handler and, conceptually, the one emitted via ws.send.

The Node codec and the Cloudflare transport both hand up a string | Uint8Array; this is the single shape they normalize onto.

Handed to you by the framework; see Inline handlers.

get isBinary(): boolean

True when the message arrived as a binary frame.

returns boolean

get raw(): string | Uint8Array

The raw payload exactly as it crossed the wire: text (string) or binary (Uint8Array).

returns string | Uint8Array

get size(): number

Byte length of the payload (UTF-8 byte length for a text payload). Memoized.

returns number

json(): JsonValue

Parses and returns the payload as JSON. Memoized (a valid null result is cached, not re-parsed).

returns JsonValue

Throws:

When the payload is not valid JSON. The memo is only set on success, so a caller that catches the error and retries re-parses rather than reading a stale undefined.

text(): string

Returns the payload as text: the string itself, or the UTF-8 decoding of a binary payload. Memoized.

Decoding is lenient (invalid sequences become the replacement character), matching FlareRequest; an inbound text frame is already validated as UTF-8 by the codec.

returns string

Learn: Inline handlers

Last updated: