Skip to main content

Signature

websocket(...) resolves after the connection opens.

Constructor shape

WebSocket also supports constructor usage for browser-style ergonomics.

WebSocketOptions

BrowserProfile
Browser fingerprint profile for the connection.
EmulationOS
Operating system fingerprint for the connection.
string
Proxy URL for the connection.
HeadersInit
Additional headers for the WebSocket handshake.
string | string[]
Optional subprotocol list for compatibility with standard WebSocket shape. Values are validated for non-empty unique entries and sent in the Sec-WebSocket-Protocol handshake header.
number
Maximum size in bytes for a single incoming WebSocket frame. Increase this when the peer sends large unfragmented frames.
number
Maximum size in bytes for a complete incoming WebSocket message. Increase this when the peer sends very large fragmented messages.
'nodebuffer' | 'arraybuffer' | 'blob'
default:"nodebuffer"
Binary payload format exposed at event.data.

Returned WebSocket instance

The instance mirrors familiar WebSocket APIs.
  1. Properties
    1. url
    2. readyState
    3. binaryType
    4. bufferedAmount
    5. protocol
    6. extensions
    7. onopen
    8. onmessage
    9. onclose
    10. onerror
protocol and extensions reflect negotiated values from the upgrade response when the server provides them.
  1. Methods
    1. send(data) where data can be string, Buffer, ArrayBuffer, ArrayBufferView, or Blob
    2. close(code?, reason?)
    3. addEventListener(type, listener)
    4. removeEventListener(type, listener)
close(code, reason) accepts code 1000 or codes in the 3000 to 4999 range. When providing a reason, the UTF-8 byte length must be 123 or fewer.
  1. Constants
    1. WebSocket.CONNECTING
    2. WebSocket.OPEN
    3. WebSocket.CLOSING
    4. WebSocket.CLOSED

Session support

For authenticated socket flows, use createSession() and then session.websocket(url, options). Within a session, WebSocket uses the same session context and transport settings.

Examples

Helper style

Constructor style

Binary payload mode

Large incoming messages

Some providers send very large WebSocket payloads in a single frame. If you see an error such as Space limit exceeded: Message too long, raise the frame or message limits explicitly.