Skip to content

BunServerWebSocket

reference
1 min readUpdated

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

PropertyType
dataT
readyState`0

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

Was this page helpful?

Download as PDF
BunServerWebSocket — Hono (narrator proof)