Current integration status
The exported WebSocketManager dispatches open, message, close, drain, and error callbacks. The core app.ws() path currently creates a placeholder manager and is not a complete end-to-end integration. Use the explicit Bun server wiring below rather than relying on app.ws().
Complete echo server
index.ts
import { Volt, WebSocketManager } from 'voltts';
const app = new Volt();
app.get('/health', () => ({ ok: true }));
const sockets = new WebSocketManager();
sockets.register('/ws', {
open(ws) { ws.send('Connected to Volt'); },
message(ws, message) { ws.send(`Echo: ${String(message)}`); },
});
if (globalThis._importMeta_.main) {
const handlers = sockets.createBunWebSocketHandler();
Bun.serve<{ path: string }>({
port: 3000,
fetch(request, server) {
if (new URL(request.url).pathname === '/ws') {
if (server.upgrade(request, { data: { path: '/ws' } })) return;
return new Response('WebSocket upgrade required', { status: 400 });
}
return app.handle(request);
},
websocket: {
...handlers,
error(error, ws) { handlers.error(ws, error); },
},
});
}
export default app;Connect a client
Run bun run index.ts, then use this code in a browser's developer console:
javascript
const socket = new WebSocket('ws://localhost:3000/ws');
socket.onmessage = (event) => console.log(event.data);
socket.onopen = () => socket.send('Hello');Expect a connection greeting and an echo. HTTP GET /health is still dispatched through Volt. This example starts Bun.serve directly, so it does not use app.listen() or automatically run Volt's startup and shutdown hooks. Configure upgrade authorization and resource limits for your application's needs.