-
Notifications
You must be signed in to change notification settings - Fork 8
Description
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
- Install SDK on Node 24
- Call
client.websockets.connect({ authToken: token }) - 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.