# BunServerWebSocket

**Kind:** Interface

**Source:** [`src/adapter/bun/websocket.ts`](https://github.com/honojs/hono/blob/main/src/adapter/bun/websocket.ts#L8)

**Part of:** [Adapter](subsystem-src-adapter)

`BunServerWebSocket<T>` represents a WebSocket connection in the Bun adapter. It exposes connection-scoped `data`, the current `readyState`, and methods for sending through or closing the connection.

## Properties

| Property | Type |
|---|---|
| `data` | `T` |
| `readyState` | `0 | 1 | 2 | 3` |

## Diagram

```mermaid
graph LR
  App[Application handler] --> Socket[BunServerWebSocket<T>]
  Socket --> Data[data: T]
  Socket --> State[readyState]
  Socket --> Send[send()]
  Socket --> Close[close()]
```

## Usage

```ts
import type { BunServerWebSocket } from "./websocket";

type SessionData = {
  userId: string;
};

function handleSocket(socket: BunServerWebSocket<SessionData>) {
  if (socket.readyState === 1) {
    socket.send();
  }

  console.log(`Connected user: ${socket.data.userId}`);

  socket.close();
}
```

## AI Coding Instructions

- Treat `data` as typed, connection-scoped state supplied by the Bun adapter.
- Check `readyState` before calling `send()` when connection state matters.
- Do not pass a payload to `send()`; this interface declares no parameters.
- Call `close()` when the application is finished with the connection.
- Keep Bun-specific WebSocket handling behind the adapter boundary.

## Relationships

- IMPORTS → `createWSMessageEvent`
- IMPORTS → `defineWebSocketHelper`
- IMPORTS → `WSContext`
- IMPORTS → `getBunServer`
