// ws://<host>/realtime/chat — a simple broadcast room.
//
// The client side is the framework's realtime runtime; a page opts in with
// `data-room="chat"`. Here we only handle room events.
//
//   client.send(msg)            → just this connection
//   client.broadcast(msg)       → everyone else in the room
//   client.room.broadcast(msg)  → everyone, including the sender
import { defineRoom } from "@wrnexus/core";

export default defineRoom({
  onConnect(client) {
    client.send({ type: "system", text: "connected" });
  },

  onMessage(client, msg) {
    // Echo each message to the whole room so every tab stays in sync.
    client.room.broadcast({ type: "message", data: msg });
  },
});
