Skip to content

WebSocket connection fails on Node.js 24 — built-in WebSocket doesn't support auth headers #12

@israelkalush-lgtm

Description

@israelkalush-lgtm

Description

The SDK's ReconnectingWebSocket (core/websocket/ws.js) uses getGlobalWebSocket() which prefers Node's built-in WebSocket (available since Node 22+) over the ws package. However, Node's built-in WebSocket does not support custom headers on connection, which means the Authorization: Bearer <token> header is never sent. The server returns 403 and the connection closes immediately.

Environment

  • Node.js v24.14.0
  • agentmail SDK v0.2.21
  • macOS (Apple Silicon)

Steps to reproduce

  1. Install SDK on Node 24
  2. Call client.websockets.connect({ authToken: token })
  3. WebSocket connects but immediately closes with code 1000/1006

Root cause

In core/websocket/ws.js, getGlobalWebSocket() checks typeof WebSocket !== "undefined" first. On Node 24, this is true (built-in), so the ws package is never used. But Node's built-in WebSocket cannot pass auth headers.

Workaround

Swap the check order to prefer ws on Node:

const getGlobalWebSocket = () => {
    if (index_js_1.RUNTIME.type === "node") {
        return ws_1.WebSocket;
    }
    else if (typeof WebSocket !== "undefined") {
        return WebSocket;
    }
    return undefined;
};

Suggested fix

Always prefer the ws package when running in a Node environment, since it supports headers. Fall back to the built-in WebSocket only in browser/Deno/Bun environments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions