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
| Method | Signature | Returns |
|---|---|---|
send | `send(source: string | ArrayBuffer |
close | close(code: number, reason: string) | void |
Properties
| Property | Type |
|---|---|
#init | WSContextInit<T> |
raw | T |
binaryType | BinaryType |
url | `URL |
protocol | `string |
Diagram
mermaidgraph LR Handler[WebSocket handler] --> Context[WSContext] Context --> Send[send()] Context --> Close[close()] Send --> Socket[WebSocket connection] Close --> Socket
Usage
tsimport 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
WSContextas 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
WSContextthrough 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)
BunServerWebSocket—src/adapter/bun/websocket.ts:8upgradeWebSocket—src/adapter/cloudflare-workers/websocket.ts:5upgradeWebSocket—src/adapter/deno/websocket.ts:4
Was this page helpful?