WebSocketMinimal

See source code
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

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

NameDescription

type

The event type to listen for

listener

The event handler function


close

Closes the WebSocket connection.

close: (code?: number, reason?: string) => void

Parameters

NameDescription

code

Optional close code (default: 1000 for normal closure)

reason

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

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

NameDescription

type

The event type to stop listening for

listener

The event handler function to remove


send

Sends a string message through the WebSocket connection.

send: (data: string) => void

Parameters

NameDescription

data

The string data to send


Prev
TLSyncLog
Next
A11yProviderProps