1# Class: WebSocket 2 3> ⚠️ Warning: the WebSocket API is experimental. 4 5Extends: [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) 6 7The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the [WebSocket spec](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) and [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455). 8 9## `new WebSocket(url[, protocol])` 10 11Arguments: 12 13* **url** `URL | string` - The url's protocol *must* be `ws` or `wss`. 14* **protocol** `string | string[] | WebSocketInit` (optional) - Subprotocol(s) to request the server use, or a [`Dispatcher`](./Dispatcher.md). 15 16### Example: 17 18This example will not work in browsers or other platforms that don't allow passing an object. 19 20```mjs 21import { WebSocket, ProxyAgent } from 'undici' 22 23const proxyAgent = new ProxyAgent('my.proxy.server') 24 25const ws = new WebSocket('wss://echo.websocket.events', { 26 dispatcher: proxyAgent, 27 protocols: ['echo', 'chat'] 28}) 29``` 30 31If you do not need a custom Dispatcher, it's recommended to use the following pattern: 32 33```mjs 34import { WebSocket } from 'undici' 35 36const ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat']) 37``` 38 39## Read More 40 41- [MDN - WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) 42- [The WebSocket Specification](https://www.rfc-editor.org/rfc/rfc6455) 43- [The WHATWG WebSocket Specification](https://websockets.spec.whatwg.org/) 44