{
  "type": "module",
  "source": "doc/api/api-websocket.md",
  "modules": [
    {
      "textRaw": "Class: WebSocket",
      "name": "class:_websocket",
      "type": "module",
      "desc": "<p>Extends: <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/EventTarget\"><code>EventTarget</code></a></p>\n<p>The WebSocket object provides a way to manage a WebSocket connection to a server, allowing bidirectional communication. The API follows the <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WebSocket\">WebSocket spec</a> and <a href=\"https://datatracker.ietf.org/doc/html/rfc6455\">RFC 6455</a>.</p>",
      "signatures": [
        {
          "textRaw": "`new WebSocket(url[, protocol])`",
          "name": "WebSocket",
          "type": "ctor",
          "params": [
            {
              "name": "url"
            },
            {
              "name": "protocol",
              "optional": true
            }
          ],
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>url</strong> <code>URL | string</code></li>\n<li><strong>protocol</strong> <code>string | string[] | WebSocketInit</code> (optional) - Subprotocol(s) to request the server use, or a <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a>.</li>\n</ul>",
          "modules": [
            {
              "textRaw": "WebSocketInit",
              "name": "websocketinit",
              "type": "module",
              "desc": "<p>When passing an object as the second argument, the following options are available:</p>\n<ul>\n<li><strong>protocols</strong> <code>string | string[]</code> (optional) - Subprotocol(s) to request the server use.</li>\n<li><strong>dispatcher</strong> <code>Dispatcher</code> (optional) - A custom <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a> to use for the connection.</li>\n<li><strong>headers</strong> <code>HeadersInit</code> (optional) - Custom headers to include in the WebSocket handshake request.</li>\n</ul>",
              "displayName": "WebSocketInit"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { WebSocket, ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: proxyAgent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>\n<p>If you do not need a custom Dispatcher, it's recommended to use the following pattern:</p>\n<pre><code class=\"language-js\">import { WebSocket } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])\n</code></pre>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example with HTTP/2:",
              "name": "example_with_http/2:",
              "type": "module",
              "desc": "<blockquote>\n<p>⚠️ Warning: WebSocket over HTTP/2 is experimental, it is likely to change in the future.</p>\n</blockquote>\n<blockquote>\n<p>🗒️ Note: WebSocket over HTTP/2 may be enabled by default in a future version,\nthis will happen by enabling HTTP/2 connections as the default behavior of Undici's Agent as well the global dispatcher.\nStay tuned to the changelog for more information.</p>\n</blockquote>\n<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { Agent } from 'undici'\n\nconst agent = new Agent({ allowH2: true })\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: agent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>",
              "displayName": "Example with HTTP/2:"
            },
            {
              "textRaw": "WebSocketInit",
              "name": "websocketinit",
              "type": "module",
              "desc": "<p>When passing an object as the second argument, the following options are available:</p>\n<ul>\n<li><strong>protocols</strong> <code>string | string[]</code> (optional) - Subprotocol(s) to request the server use.</li>\n<li><strong>dispatcher</strong> <code>Dispatcher</code> (optional) - A custom <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a> to use for the connection.</li>\n<li><strong>headers</strong> <code>HeadersInit</code> (optional) - Custom headers to include in the WebSocket handshake request.</li>\n</ul>",
              "displayName": "WebSocketInit"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { WebSocket, ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: proxyAgent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>\n<p>If you do not need a custom Dispatcher, it's recommended to use the following pattern:</p>\n<pre><code class=\"language-js\">import { WebSocket } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])\n</code></pre>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example with HTTP/2:",
              "name": "example_with_http/2:",
              "type": "module",
              "desc": "<blockquote>\n<p>⚠️ Warning: WebSocket over HTTP/2 is experimental, it is likely to change in the future.</p>\n</blockquote>\n<blockquote>\n<p>🗒️ Note: WebSocket over HTTP/2 may be enabled by default in a future version,\nthis will happen by enabling HTTP/2 connections as the default behavior of Undici's Agent as well the global dispatcher.\nStay tuned to the changelog for more information.</p>\n</blockquote>\n<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { Agent } from 'undici'\n\nconst agent = new Agent({ allowH2: true })\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: agent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>",
              "displayName": "Example with HTTP/2:"
            }
          ]
        },
        {
          "textRaw": "`new WebSocket(url[, protocol])`",
          "name": "WebSocket",
          "type": "ctor",
          "params": [
            {
              "name": "url"
            },
            {
              "name": "protocol",
              "optional": true
            }
          ],
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>url</strong> <code>URL | string</code></li>\n<li><strong>protocol</strong> <code>string | string[] | WebSocketInit</code> (optional) - Subprotocol(s) to request the server use, or a <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a>.</li>\n</ul>",
          "modules": [
            {
              "textRaw": "WebSocketInit",
              "name": "websocketinit",
              "type": "module",
              "desc": "<p>When passing an object as the second argument, the following options are available:</p>\n<ul>\n<li><strong>protocols</strong> <code>string | string[]</code> (optional) - Subprotocol(s) to request the server use.</li>\n<li><strong>dispatcher</strong> <code>Dispatcher</code> (optional) - A custom <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a> to use for the connection.</li>\n<li><strong>headers</strong> <code>HeadersInit</code> (optional) - Custom headers to include in the WebSocket handshake request.</li>\n</ul>",
              "displayName": "WebSocketInit"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { WebSocket, ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: proxyAgent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>\n<p>If you do not need a custom Dispatcher, it's recommended to use the following pattern:</p>\n<pre><code class=\"language-js\">import { WebSocket } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])\n</code></pre>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example with HTTP/2:",
              "name": "example_with_http/2:",
              "type": "module",
              "desc": "<blockquote>\n<p>⚠️ Warning: WebSocket over HTTP/2 is experimental, it is likely to change in the future.</p>\n</blockquote>\n<blockquote>\n<p>🗒️ Note: WebSocket over HTTP/2 may be enabled by default in a future version,\nthis will happen by enabling HTTP/2 connections as the default behavior of Undici's Agent as well the global dispatcher.\nStay tuned to the changelog for more information.</p>\n</blockquote>\n<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { Agent } from 'undici'\n\nconst agent = new Agent({ allowH2: true })\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: agent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>",
              "displayName": "Example with HTTP/2:"
            },
            {
              "textRaw": "WebSocketInit",
              "name": "websocketinit",
              "type": "module",
              "desc": "<p>When passing an object as the second argument, the following options are available:</p>\n<ul>\n<li><strong>protocols</strong> <code>string | string[]</code> (optional) - Subprotocol(s) to request the server use.</li>\n<li><strong>dispatcher</strong> <code>Dispatcher</code> (optional) - A custom <a href=\"/docs/docs/api/Dispatcher.html\"><code>Dispatcher</code></a> to use for the connection.</li>\n<li><strong>headers</strong> <code>HeadersInit</code> (optional) - Custom headers to include in the WebSocket handshake request.</li>\n</ul>",
              "displayName": "WebSocketInit"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { WebSocket, ProxyAgent } from 'undici'\n\nconst proxyAgent = new ProxyAgent('my.proxy.server')\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: proxyAgent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>\n<p>If you do not need a custom Dispatcher, it's recommended to use the following pattern:</p>\n<pre><code class=\"language-js\">import { WebSocket } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events', ['echo', 'chat'])\n</code></pre>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example with HTTP/2:",
              "name": "example_with_http/2:",
              "type": "module",
              "desc": "<blockquote>\n<p>⚠️ Warning: WebSocket over HTTP/2 is experimental, it is likely to change in the future.</p>\n</blockquote>\n<blockquote>\n<p>🗒️ Note: WebSocket over HTTP/2 may be enabled by default in a future version,\nthis will happen by enabling HTTP/2 connections as the default behavior of Undici's Agent as well the global dispatcher.\nStay tuned to the changelog for more information.</p>\n</blockquote>\n<p>This example will not work in browsers or other platforms that don't allow passing an object.</p>\n<pre><code class=\"language-js\">import { Agent } from 'undici'\n\nconst agent = new Agent({ allowH2: true })\n\nconst ws = new WebSocket('wss://echo.websocket.events', {\n  dispatcher: agent,\n  protocols: ['echo', 'chat']\n})\n</code></pre>",
              "displayName": "Example with HTTP/2:"
            }
          ]
        }
      ],
      "displayName": "Class: WebSocket"
    },
    {
      "textRaw": "Class: WebSocketStream",
      "name": "class:_websocketstream",
      "type": "module",
      "desc": "<blockquote>\n<p>⚠️ Warning: the WebSocketStream API has not been finalized and is likely to change.</p>\n</blockquote>\n<p>See <a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WebSocketStream\">MDN</a> for more information.</p>",
      "signatures": [
        {
          "textRaw": "`new WebSocketStream(url[, protocol])`",
          "name": "WebSocketStream",
          "type": "ctor",
          "params": [
            {
              "name": "url"
            },
            {
              "name": "protocol",
              "optional": true
            }
          ],
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>url</strong> <code>URL | string</code></li>\n<li><strong>options</strong> <code>WebSocketStreamOptions</code> (optional)</li>\n</ul>",
          "modules": [
            {
              "textRaw": "WebSocketStream Example",
              "name": "websocketstream_example",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">const stream = new WebSocketStream('https://echo.websocket.org/')\nconst { readable, writable } = await stream.opened\n\nasync function read () {\n  /** @type {ReadableStreamReader} */\n  const reader = readable.getReader()\n\n  while (true) {\n    const { done, value } = await reader.read()\n    if (done) break\n\n    // do something with value\n  }\n}\n\nasync function write () {\n  /** @type {WritableStreamDefaultWriter} */\n  const writer = writable.getWriter()\n  writer.write('Hello, world!')\n  writer.releaseLock()\n}\n\nread()\n\nsetInterval(() => write(), 5000)\n\n</code></pre>",
              "displayName": "WebSocketStream Example"
            },
            {
              "textRaw": "WebSocketStream Example",
              "name": "websocketstream_example",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">const stream = new WebSocketStream('https://echo.websocket.org/')\nconst { readable, writable } = await stream.opened\n\nasync function read () {\n  /** @type {ReadableStreamReader} */\n  const reader = readable.getReader()\n\n  while (true) {\n    const { done, value } = await reader.read()\n    if (done) break\n\n    // do something with value\n  }\n}\n\nasync function write () {\n  /** @type {WritableStreamDefaultWriter} */\n  const writer = writable.getWriter()\n  writer.write('Hello, world!')\n  writer.releaseLock()\n}\n\nread()\n\nsetInterval(() => write(), 5000)\n\n</code></pre>",
              "displayName": "WebSocketStream Example"
            }
          ]
        },
        {
          "textRaw": "`new WebSocketStream(url[, protocol])`",
          "name": "WebSocketStream",
          "type": "ctor",
          "params": [
            {
              "name": "url"
            },
            {
              "name": "protocol",
              "optional": true
            }
          ],
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>url</strong> <code>URL | string</code></li>\n<li><strong>options</strong> <code>WebSocketStreamOptions</code> (optional)</li>\n</ul>",
          "modules": [
            {
              "textRaw": "WebSocketStream Example",
              "name": "websocketstream_example",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">const stream = new WebSocketStream('https://echo.websocket.org/')\nconst { readable, writable } = await stream.opened\n\nasync function read () {\n  /** @type {ReadableStreamReader} */\n  const reader = readable.getReader()\n\n  while (true) {\n    const { done, value } = await reader.read()\n    if (done) break\n\n    // do something with value\n  }\n}\n\nasync function write () {\n  /** @type {WritableStreamDefaultWriter} */\n  const writer = writable.getWriter()\n  writer.write('Hello, world!')\n  writer.releaseLock()\n}\n\nread()\n\nsetInterval(() => write(), 5000)\n\n</code></pre>",
              "displayName": "WebSocketStream Example"
            },
            {
              "textRaw": "WebSocketStream Example",
              "name": "websocketstream_example",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">const stream = new WebSocketStream('https://echo.websocket.org/')\nconst { readable, writable } = await stream.opened\n\nasync function read () {\n  /** @type {ReadableStreamReader} */\n  const reader = readable.getReader()\n\n  while (true) {\n    const { done, value } = await reader.read()\n    if (done) break\n\n    // do something with value\n  }\n}\n\nasync function write () {\n  /** @type {WritableStreamDefaultWriter} */\n  const writer = writable.getWriter()\n  writer.write('Hello, world!')\n  writer.releaseLock()\n}\n\nread()\n\nsetInterval(() => write(), 5000)\n\n</code></pre>",
              "displayName": "WebSocketStream Example"
            }
          ]
        }
      ],
      "modules": [
        {
          "textRaw": "ping(websocket, payload)",
          "name": "ping(websocket,_payload)",
          "type": "module",
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>websocket</strong> <code>WebSocket</code> - The WebSocket instance to send the ping frame on</li>\n<li><strong>payload</strong> <code>Buffer|undefined</code> (optional) - Optional payload data to include with the ping frame. Must not exceed 125 bytes.</li>\n</ul>\n<p>Sends a ping frame to the WebSocket server. The server must respond with a pong frame containing the same payload data. This can be used for keepalive purposes or to verify that the connection is still active.</p>",
          "modules": [
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">import { WebSocket, ping } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events')\n\nws.addEventListener('open', () => {\n  // Send ping with no payload\n  ping(ws)\n\n  // Send ping with payload\n  const payload = Buffer.from('hello')\n  ping(ws, payload)\n})\n</code></pre>\n<p><strong>Note</strong>: A ping frame cannot have a payload larger than 125 bytes. The ping will only be sent if the WebSocket connection is in the OPEN state.</p>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">import { WebSocket, ping } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events')\n\nws.addEventListener('open', () => {\n  // Send ping with no payload\n  ping(ws)\n\n  // Send ping with payload\n  const payload = Buffer.from('hello')\n  ping(ws, payload)\n})\n</code></pre>\n<p><strong>Note</strong>: A ping frame cannot have a payload larger than 125 bytes. The ping will only be sent if the WebSocket connection is in the OPEN state.</p>",
              "displayName": "Example:"
            }
          ],
          "displayName": "ping(websocket, payload)"
        },
        {
          "textRaw": "Read More",
          "name": "read_more",
          "type": "module",
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WebSocket\">MDN - WebSocket</a></li>\n<li><a href=\"https://www.rfc-editor.org/rfc/rfc6455\">The WebSocket Specification</a></li>\n<li><a href=\"https://websockets.spec.whatwg.org/\">The WHATWG WebSocket Specification</a></li>\n</ul>",
          "displayName": "Read More"
        },
        {
          "textRaw": "ping(websocket, payload)",
          "name": "ping(websocket,_payload)",
          "type": "module",
          "desc": "<p>Arguments:</p>\n<ul>\n<li><strong>websocket</strong> <code>WebSocket</code> - The WebSocket instance to send the ping frame on</li>\n<li><strong>payload</strong> <code>Buffer|undefined</code> (optional) - Optional payload data to include with the ping frame. Must not exceed 125 bytes.</li>\n</ul>\n<p>Sends a ping frame to the WebSocket server. The server must respond with a pong frame containing the same payload data. This can be used for keepalive purposes or to verify that the connection is still active.</p>",
          "modules": [
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">import { WebSocket, ping } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events')\n\nws.addEventListener('open', () => {\n  // Send ping with no payload\n  ping(ws)\n\n  // Send ping with payload\n  const payload = Buffer.from('hello')\n  ping(ws, payload)\n})\n</code></pre>\n<p><strong>Note</strong>: A ping frame cannot have a payload larger than 125 bytes. The ping will only be sent if the WebSocket connection is in the OPEN state.</p>",
              "displayName": "Example:"
            },
            {
              "textRaw": "Example:",
              "name": "example:",
              "type": "module",
              "desc": "<pre><code class=\"language-js\">import { WebSocket, ping } from 'undici'\n\nconst ws = new WebSocket('wss://echo.websocket.events')\n\nws.addEventListener('open', () => {\n  // Send ping with no payload\n  ping(ws)\n\n  // Send ping with payload\n  const payload = Buffer.from('hello')\n  ping(ws, payload)\n})\n</code></pre>\n<p><strong>Note</strong>: A ping frame cannot have a payload larger than 125 bytes. The ping will only be sent if the WebSocket connection is in the OPEN state.</p>",
              "displayName": "Example:"
            }
          ],
          "displayName": "ping(websocket, payload)"
        },
        {
          "textRaw": "Read More",
          "name": "read_more",
          "type": "module",
          "desc": "<ul>\n<li><a href=\"https://developer.mozilla.org/en-US/docs/Web/API/WebSocket\">MDN - WebSocket</a></li>\n<li><a href=\"https://www.rfc-editor.org/rfc/rfc6455\">The WebSocket Specification</a></li>\n<li><a href=\"https://websockets.spec.whatwg.org/\">The WHATWG WebSocket Specification</a></li>\n</ul>",
          "displayName": "Read More"
        }
      ],
      "displayName": "Class: WebSocketStream"
    }
  ]
}