WebSocketMinimal
Table of contents
Minimal server-side WebSocket interface that is compatible with various WebSocket implementations. This interface abstracts over different WebSocket libraries and platforms to provide a consistent API for the ServerSocketAdapter.
Supports: - The standard WebSocket interface (Cloudflare, Deno, some Node.js setups) - The 'ws' WebSocket interface (Node.js ws library) - The Bun.serve socket implementation
interface WebSocketMinimal {}Example
// Standard WebSocket
const standardWs: WebSocketMinimal = new WebSocket("ws://localhost:8080");
// Node.js 'ws' library WebSocket
import WebSocket from "ws";
const nodeWs: WebSocketMinimal = new WebSocket("ws://localhost:8080");
// Bun WebSocket (in server context)
// const bunWs: WebSocketMinimal = server.upgrade(request)Properties
addEventListener
Optional method to add event listeners for WebSocket events. Not all WebSocket implementations provide this method.
addEventListener?: (
type: "close" | "error" | "message",
listener: (event: any) => void,
) => void;Parameters
| Name | Description |
|---|---|
| The event type to listen for |
| The event handler function |
close
Closes the WebSocket connection.
close: (code?: number, reason?: string) => void;Parameters
| Name | Description |
|---|---|
| Optional close code (default: 1000 for normal closure) |
| Optional human-readable close reason |
readyState
The current state of the WebSocket connection. - 0: CONNECTING - 1: OPEN - 2: CLOSING - 3: CLOSED
readyState: number;removeEventListener
Optional method to remove event listeners for WebSocket events. Not all WebSocket implementations provide this method.
removeEventListener?: (
type: "close" | "error" | "message",
listener: (event: any) => void,
) => void;Parameters
| Name | Description |
|---|---|
| The event type to stop listening for |
| The event handler function to remove |
send
Sends a string message through the WebSocket connection.
send: (data: string) => void;Parameters
| Name | Description |
|---|---|
| The string data to send |