// ws:///realtime/chat — a broadcast chat room. // // The whole client side is the framework's realtime runtime (the /chat page just // declares `data-room="chat"`). Here we only handle room events. import { defineRoom } from "@wrnexus/core"; type ChatIn = { user?: string; text?: string }; export default defineRoom({ onConnect(client) { // Tell everyone else someone joined (the joiner sees the status badge). client.broadcast({ type: "system", text: "A user joined", online: client.room.count() }); }, onMessage(client, msg: ChatIn) { const text = String(msg.text ?? "") .slice(0, 500) .trim(); if (!text) return; const user = String(msg.user ?? "anon").slice(0, 40) || "anon"; // Broadcast to the whole room, including the sender, so everyone stays in sync. client.room.broadcast({ type: "message", user, text }); }, onLeave(client) { client.broadcast({ type: "system", text: "A user left", online: client.room.count() - 1 }); }, });