Kind: Interface
Source: src/adapter/bun/websocket.ts
Part of: 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 |
Diagram
mermaidgraph LR App[Application handler] --> Socket[BunServerWebSocket<T>] Socket --> Data[data: T] Socket --> State[readyState] Socket --> Send[send()] Socket --> Close[close()]
Usage
tsimport 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
dataas typed, connection-scoped state supplied by the Bun adapter. - Check
readyStatebefore callingsend()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
Was this page helpful?