Skip to main content
Version: v8.0.0

WebsocketAdapterOptionsType


Import
import { WebsocketAdapterOptionsType } from "@hyper-fetch/sockets"

Preview​

type WebsocketAdapterOptionsType = {
autoConnect?: boolean;
decode?: (event: MessageEvent) => WebsocketDecodedMessageType | null;
encode?: (message: WebsocketEncodeMessageType) => WebsocketFrameType;
heartbeat?: boolean;
heartbeatMessage?: string;
pingTimeout?: number;
pongTimeout?: number;
protocols?: string | string[];
}

Structure​

WebsocketAdapterOptionsType
NameTypeDescription
autoConnect
boolean
heartbeat
boolean
heartbeatMessage
string
pingTimeout
number
pongTimeout
number
protocols
string | string[]

Subprotocols passed to the WebSocket constructor ( Sec-WebSocket-Protocol header). To resolve them per connection attempt (e.g. short-lived auth tokens) use socket.onConnect .

decode
(event: MessageEvent) => WebsocketDecodedMessageType | null

Turns an incoming frame into a listener event - use it when the server does not send { topic, data }

(e.g. { type, payload } ). Return null to ignore the frame (pings, unknown messages). Defaults to parsing the frame as JSON and reading its topic and data fields.

encode
(message: WebsocketEncodeMessageType) => WebsocketFrameType

Turns an emitted message (and the heartbeat ping) into the frame passed to websocket.send . Defaults to JSON.stringify({ topic, data: payload }) .