Skip to main content
wreq-js supports both a convenience helper and a constructor-style API.

1. Quick connection with the helper

Use websocket(url, options) when you want a connected socket from a single await.

2. Constructor style

Use new WebSocket(...) when you want browser-like shape with CONNECTING state.

3. Sessions, impersonation, and cookies

For authenticated flows, create a session, log in with HTTP, then open the WebSocket with the same session. session.websocket(...) reuses the session transport and context.

4. Event model

Event handlers and listeners follow familiar WebSocket patterns.

5. Binary messages

binaryType defaults to nodebuffer. You can set it to arraybuffer or blob.

6. Close behavior

Use close() or close(code, reason).
If you pass a custom close code, use 1000 or a code in the 3000 to 4999 range. If you pass a reason, keep it to 123 UTF-8 bytes or fewer.

7. Large frames and message limits

Incoming WebSocket data is bounded by default to avoid unbounded memory use. If a provider sends a very large payload in one frame, you may see an error like Space limit exceeded: Message too long. Raise maxFrameSize when the peer sends large unfragmented frames. Raise maxMessageSize when the peer sends very large fragmented messages.
  1. API reference: websocket()
  2. Session API: createSession()
  3. Session concepts: /concepts/sessions