Defjs
Core

withWebSocketBeforeConnect

Runs a hook before the initial WebSocket connect and before every reconnect.

@defjs/core · Function

Signature

function withWebSocketBeforeConnect(
  beforeConnect: ClientWebSocketOptions["beforeConnect"],
): ClientOption;

The hook type is:

(context: { attempt: number; signal: AbortSignal }) => void | Promise<void>
  • attempt is 0 for the first connect and grows by one for each reconnect.
  • Pass signal into token refreshes and other async work so cancelling the session stops them.
  • If the hook throws, the attempt fails with an EXT_HOOK_FAILED fault.
  • A beforeConnect in the request options replaces this one for that call.

Parameters

Prop

Type

beforeConnect ((context: { attempt: number; signal: AbortSignal; }) => void | Promise<void>) | undefined

Hook receiving attempt index and abort signal.

Returns

An option that sets config.webSocket.beforeConnect.

Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao