Undici implements the WHATWG WebSocket API as defined in the browser spec, giving Node.js applications the sameDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/nodejs/undici/llms.txt
Use this file to discover all available pages before exploring further.
WebSocket interface available in browsers — without any CORS restrictions. The implementation follows RFC 6455 and supports text and binary frames, subprotocols, custom dispatchers (including proxy agents), and HTTP/2 upgrades. A streaming variant, WebSocketStream, is also available for backpressure-aware consumption.
Basic connection
Opening a WebSocket connection
Constructor
| Argument | Type | Description |
|---|---|---|
url | string | URL | The WebSocket server URL (ws:// or wss://) |
protocols | string | string[] | WebSocketInit | Subprotocol(s) or a WebSocketInit options object |
WebSocketInit options object
Passing an object as the second argument gives access to additional options not available in the browser WebSocket API:
| Option | Type | Description |
|---|---|---|
protocols | string | string[] | Subprotocol(s) to request |
dispatcher | Dispatcher | A custom undici dispatcher (e.g. ProxyAgent) |
headers | HeadersInit | Custom headers to include in the WebSocket handshake |
WebSocket with custom dispatcher and protocols
WebSocket with subprotocols only
Event listeners
WebSocket fires four events. You can assign handlers viaonXxx properties or via addEventListener:
- onXxx handlers
- addEventListener
readyState constants
| Constant | Value | Meaning |
|---|---|---|
WebSocket.CONNECTING | 0 | Connection not yet established |
WebSocket.OPEN | 1 | Connection established; data can be sent |
WebSocket.CLOSING | 2 | Close handshake in progress |
WebSocket.CLOSED | 3 | Connection is closed |
Sending messages
Callws.send(data) after the connection is open. Supported data types are string, ArrayBuffer, typed arrays, and Blob.
Sending text and binary data
Receiving messages
Incoming messages arrive on themessage event. Binary messages are delivered as a Blob by default; change ws.binaryType to 'arraybuffer' to receive them as ArrayBuffer instead.
Receiving text and binary messages
Closing connections
Callws.close([code[, reason]]) to initiate a clean WebSocket closing handshake. The close event fires when the connection is fully closed.
Closing with code and reason
Common close codes:
1000 (normal), 1001 (going away), 1006 (abnormal — no close frame received), 1011 (server error). The close reason must be a UTF-8 string of at most 123 bytes.Sending ping frames
Use the exportedping() function to send a WebSocket ping frame for keepalive or connection verification. The server must reply with a pong frame containing the same payload.
Sending a ping
A ping frame payload cannot exceed 125 bytes. Pings are silently ignored if the connection is not in the
OPEN state.WebSocketStream for stream-based access
WebSocketStream provides a streams-based interface with built-in backpressure support. The API is based on the WHATWG WebSocketStream proposal and is still experimental.
WebSocketStream example
HTTP/2 WebSocket connections
WebSocket over HTTP/2 is supported experimentally. Pass anAgent with allowH2: true as the dispatcher:
WebSocket over HTTP/2
Difference from browser WebSocket
Undici’sWebSocket is intentionally API-compatible with the browser’s WebSocket global, but it runs in Node.js so a few differences apply:
- No CORS restrictions — server-side code can connect to any origin
- Custom dispatchers — pass a
ProxyAgentor other dispatcher viaWebSocketInit - Custom headers — set arbitrary handshake headers via
WebSocketInit.headers(browsers don’t allow this) ping()function — browsers do not expose an API to send raw ping frames; undici provides one as a named export
Custom handshake headers (Node.js only)