// server.js (for Bun) import { serve } from "bun"; const rooms = new Map(); // roomId β†’ Set serve({ port: Number(process.env.PORT) || 7860, // Serve the HTML page at β€œ/” async fetch(req) { const url = new URL(req.url); if (url.pathname !== "/") return new Response("Not Found", { status: 404 }); return new Response(` Dubem Realtime Rooms

Join a Room & Send Messages (truly 1)

`, { headers: { "Content-Type": "text/html; charset=utf-8" }, }); }, // Hook into Bun's WebSocket handling websocket: { open(ws) { // no-op here; clients join via β€œjoin” action }, message(ws, raw) { let msg; try { msg = JSON.parse(raw.toString()); } catch { return; } if (msg.action === "join" && msg.roomId) { // remove from all rooms for (const clients of rooms.values()) { clients.delete(ws); } // add to new room const roomSet = rooms.get(msg.roomId) || new Set(); roomSet.add(ws); rooms.set(msg.roomId, roomSet); return; } if (msg.action === "post" && msg.roomId && msg.message) { const clients = rooms.get(msg.roomId); if (!clients) return; const payload = JSON.stringify({ roomId: msg.roomId, message: msg.message, timestamp: Date.now(), }); for (const client of clients) { if (client.readyState === 1) { // OPEN client.send(payload); } } } }, close(ws) { // on disconnect, remove from all rooms for (const clients of rooms.values()) { clients.delete(ws); } }, }, }); console.log("βœ… Bun realtime server running on port " + (process.env.PORT || 7860));