Bun Adapter
The @zocket/server package ships with a first-class Bun adapter.
Quick Start
Section titled “Quick Start”import { serve } from "@zocket/server/bun";import { app } from "./app";
const server = serve(app, { port: 3000 });console.log(`Zocket on ws://localhost:${server.port}`);That’s it. serve() creates a Bun HTTP server that upgrades WebSocket connections and wires up all the actor message handling.
Options
Section titled “Options”serve(app, { port: 3000, // default: 0 (random available port) hostname: "0.0.0.0",});The return value is a standard Bun.Server instance.
Custom Setup with createBunHandlers
Section titled “Custom Setup with createBunHandlers”For more control (e.g., adding HTTP routes alongside WebSocket), use createBunHandlers:
import { createBunHandlers } from "@zocket/server/bun";import { app } from "./app";
const zocket = createBunHandlers(app);
Bun.serve({ port: 3000,
fetch(req, server) { // Try WebSocket upgrade first const wsResponse = zocket.fetch(req, server); if (wsResponse === undefined) return wsResponse; // upgrade succeeded
// Custom HTTP routes const url = new URL(req.url); if (url.pathname === "/health") { return new Response("ok"); }
return new Response("Not Found", { status: 404 }); },
websocket: zocket.websocket,});BunHandlers Shape
Section titled “BunHandlers Shape”interface BunHandlers { fetch(req: Request, server: BunServer): Response | undefined; websocket: WebSocketHandler;}fetch— attempts to upgrade the request to WebSocket. Returnsundefinedon success, or aResponseon failure.websocket— Bun’sWebSocketHandlerwithopen,message, andclosecallbacks wired to Zocket’s handler.
How It Works
Section titled “How It Works”Under the hood:
createBunHandlers(app)callscreateHandlers(app)to get runtime-agnostic callbacks- Each WebSocket connection gets a
BunConnectionAdapterwith a uniqueid - The adapter implements the
Connectioninterface (send()+id) - Messages are routed through the handler to the
ActorManager
Connection IDs
Section titled “Connection IDs”Each connection gets a unique ID like bun_1_m3abc. This ID is:
- Passed to method handlers as
connectionId - Used by lifecycle hooks (
onConnect,onDisconnect) - Stable for the lifetime of a WebSocket connection