Skip to content

WSContext

reference
1 min readUpdated

Kind: Class

Source: src/helper/websocket/index.ts

Part of: Helper

A context for controlling WebSockets

WSContext is a context object for controlling a WebSocket connection. It exposes methods to send through the connection and close it when the connection should end.

Methods

MethodSignatureReturns
send`send(source: stringArrayBuffer
closeclose(code: number, reason: string)void

Properties

PropertyType
#initWSContextInit<T>
rawT
binaryTypeBinaryType
url`URL
protocol`string

Diagram

mermaid
graph LR
  Handler[WebSocket handler] --> Context[WSContext]
  Context --> Send[send()]
  Context --> Close[close()]
  Send --> Socket[WebSocket connection]
  Close --> Socket

Usage

ts
import type { WSContext } from './helper/websocket'

function handleWebSocket(context: WSContext) {
  context.send()

  // Close the connection when no further messages should be sent.
  context.close()
}

AI Coding Instructions

  • Treat WSContext as the interface for sending through and closing the active WebSocket connection.
  • Call send() only while the connection is expected to remain open.
  • Call close() when the handler has finished with the connection.
  • Pass the existing WSContext through WebSocket handling code instead of creating alternate connection controls.

Used by

3 references from 3 files. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

Imported by (3)

  • BunServerWebSocketsrc/adapter/bun/websocket.ts:8
  • upgradeWebSocketsrc/adapter/cloudflare-workers/websocket.ts:5
  • upgradeWebSocketsrc/adapter/deno/websocket.ts:4

Was this page helpful?

Download as PDF
WSContext — Hono (narrator proof)