Skip to content

Latest commit

 

History

History
85 lines (55 loc) · 2.29 KB

WebSocket.md

File metadata and controls

85 lines (55 loc) · 2.29 KB

Class: WebSocket

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
  • 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'])

Class: WebSocketStream

⚠️ Warning: the WebSocketStream API has not been finalized and is likely to change.

See MDN for more information.

new WebSocketStream(url[, protocol])

Arguments:

  • url URL | string
  • options WebSocketStreamOptions (optional)

WebSocketStream Example

const stream = new WebSocketStream('https://echo.websocket.org/')
const { readable, writable } = await stream.opened

async function read () {
  /** @type {ReadableStreamReader} */
  const reader = readable.getReader()

  while (true) {
    const { done, value } = await reader.read()
    if (done) break

    // do something with value
  }
}

async function write () {
  /** @type {WritableStreamDefaultWriter} */
  const writer = writable.getWriter()
  writer.write('Hello, world!')
  writer.releaseLock()
}

read()

setInterval(() => write(), 5000)

Read More