Core
withWebSocketHeartbeat
Enables WebSocket heartbeat messages with optional acknowledgement detection.
@defjs/core · Function
Signature
function withWebSocketHeartbeat( options: ClientWebSocketOptions["heartbeat"], ): ClientOption;
Passing undefined clears an earlier setting. The option type is:
{
intervalMs: number
isAck?: (message: unknown) => boolean
message?: <T = unknown>() => T | unknown
timeoutMs?: number
}- Every
intervalMs,message()is called and its result is sent through the endpoint'soutgoingstruct. No message is sent whenmessageis omitted or returnsundefined. - With
timeoutMs, an acknowledgement is expected after each heartbeat.isAckreceives the decoded incoming message; when it returnstrue, the timer is cleared and that message is not delivered toreceive. - If no ack arrives within
timeoutMs, or a heartbeat cannot be sent, the session fails. This does not trigger a reconnect. - Heartbeat uses the same JSON guard as
session.send: a value JSON cannot represent after struct encoding causesTypeErrorand ends the logical session. intervalMsandtimeoutMsmust be positive, finite, and at most2_147_483_647; otherwise the operation fails withREQ_OPTIONS_INVALID.- A
heartbeatin the request options replaces this one for that call.
Parameters
Prop
Type
options ClientWebSocketOptions["heartbeat"]Heartbeat interval, payload, timeout, and ACK predicate.
Returns
An option that sets config.webSocket.heartbeat.