Files
send-mail/node_modules/undici/docs/docs/api/WebSocket.md
Copilot afe9786629 Run npm ci --ignore-scripts to update dependencies (#254)
* Initial plan

* Run npm ci --ignore-scripts to update dependencies

Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>

* Convert CommonJS to ESM (#255)

* Initial plan

* Convert CommonJS imports to ESM

Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>

* Use node: protocol prefix for built-in modules

Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: dawidd6 <9713907+dawidd6@users.noreply.github.com>
2026-01-30 13:31:20 +01:00

1.5 KiB

Class: WebSocket

⚠️ Warning: the WebSocket API is experimental.

Extends: EventTarget

The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the WebSocket spec and RFC 6455.

new WebSocket(url[, protocol])

Arguments:

  • url URL | string - The url's protocol must be ws or wss.
  • protocol string | string[] | WebSocketInit (optional) - Subprotocol(s) to request the server use, or a Dispatcher.

Example:

This example will not work in browsers or other platforms that don't allow passing an object.

import { WebSocket, ProxyAgent } from 'undici'

const proxyAgent = new ProxyAgent('my.proxy.server')

const ws = new WebSocket('wss://echo.websocket.events', {
  dispatcher: proxyAgent,
  protocols: ['echo', 'chat']
})

If you do not need a custom Dispatcher, it's recommended to use the following pattern:

import { WebSocket } from 'undici'

const ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])

Read More