# WSContext

**Kind:** Class

**Source:** [`src/helper/websocket/index.ts`](https://github.com/honojs/hono/blob/main/src/helper/websocket/index.ts#L70)

**Part of:** [Helper](subsystem-src-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 | Uint8Array<ArrayBuffer>, options: SendOptions)` | `void` |
| `close` | `close(code: number, reason: string)` | `void` |

## Properties

| Property | Type |
|---|---|
| `#init` | `WSContextInit<T>` |
| `raw` | `T` |
| `binaryType` | `BinaryType` |
| `url` | `URL | null` |
| `protocol` | `string | null` |

## 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)

- `BunServerWebSocket` — `src/adapter/bun/websocket.ts`:8
- `upgradeWebSocket` — `src/adapter/cloudflare-workers/websocket.ts`:5
- `upgradeWebSocket` — `src/adapter/deno/websocket.ts`:4
