Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ayonli_jsext/workerd/ws.ts>WebSocketConnection

A JavaScript extension package for building strong and modern applications.
Latest
class WebSocketConnection
implements AsyncIterable<string | Uint8Array>
extends EventTarget
Re-export
import { WebSocketConnection } from "https://deno.land/x/ayonli_jsext@v0.9.72/workerd/ws.ts";

This class represents a WebSocket connection on the server side. Normally we don't create instances of this class directly, but rather use the WebSocketServer to handle WebSocket connections, which will create the instance for us.

Events:

  • open - Dispatched when the connection is ready.
  • message - Dispatched when a message is received.
  • error - Dispatched when an error occurs, such as network failure. After this event is dispatched, the connection will be closed and the close event will be dispatched.
  • close - Dispatched when the connection is closed. If the connection is closed due to some error, the error event will be dispatched before this event, and the close event will have the wasClean set to false, and the reason property contains the error message, if any.

Constructors

new
WebSocketConnection(source: Promise<WebSocketLike>)

Properties

private
[_source]: Promise<WebSocketLike>
private
[_ws]: WebSocketLike | null
deprecated
readonly
ready: Promise<this>

A promise that resolves when the connection is ready to send and receive messages.

readonly
readyState: number

The current state of the WebSocket connection.

Methods

addEventListener(
type: "open",
listener: (this: WebSocketConnection, ev: Event) => void,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener that will be called when the connection is ready.

addEventListener(
type: "message",
listener: (this: WebSocketConnection, ev: MessageEvent<string | Uint8Array>) => void,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener that will be called when a message is received.

addEventListener(
type: "error",
listener: (this: WebSocketConnection, ev: ErrorEvent) => void,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener that will be called when the connection is interrupted. After this event is dispatched, the connection will be closed and the close event will be dispatched.

addEventListener(
type: "close",
listener: (this: WebSocketConnection, ev: CloseEvent) => void,
options?: boolean | AddEventListenerOptions,
): void

Adds an event listener that will be called when the connection is closed. If the connection is closed due to some error, the error event will be dispatched before this event, and the close event will have the wasClean set to false, and the reason property contains the error message, if any.

addEventListener(
type: string,
options?: boolean | AddEventListenerOptions,
): void
close(code?: number | undefined, reason?: string | undefined): void

Closes the WebSocket connection.

removeEventListener(
type: "open",
listener: (this: WebSocketConnection, ev: Event) => void,
options?: boolean | EventListenerOptions,
): void
removeEventListener(
type: "message",
listener: (this: WebSocketConnection, ev: MessageEvent<string | Uint8Array>) => void,
options?: boolean | EventListenerOptions,
): void
removeEventListener(
type: "error",
listener: (this: WebSocketConnection, ev: ErrorEvent) => void,
options?: boolean | EventListenerOptions,
): void
removeEventListener(
type: "close",
listener: (this: WebSocketConnection, ev: CloseEvent) => void,
options?: boolean | EventListenerOptions,
): void
removeEventListener(
type: string,
options?: boolean | EventListenerOptions,
): void
send(data: string | ArrayBufferLike | ArrayBufferView): void

Sends data to the WebSocket client.

[Symbol.asyncIterator](): AsyncIterableIterator<string | Uint8Array>