Creating Rooms
Rooms are the channels that WebSockets can enter and leave (i.e. connect and disconnect). Events emitted by sockets in a room will be broadcasted only to other sockets within the same room.
Creating a PluvRoom
To create a PluvRoom
, you must first have a PluvClient
defined. Refer to the create client documentation to do so.
1// frontend/room.ts23import { y } from "@pluv/client";4import { z } from "zod";5import { client } from "./io";67const ROOM_NAME = "my-test-room";89export const room = client.createRoom(ROOM_NAME, {10 // Define your presence schema11 presence: z.object({12 selectionId: z.nullable(z.string()),13 }),14 // Define the user's initial presence value15 initialPresence: {16 selectionId: null,17 },18 // Define the initial storage for the room19 initialStorage: () => ({20 messages: y.array(["hello world!"]),21 }),22});
Connect to your PluvRoom
1import { client } from "./io";23// connect by room name4await client.enter(ROOM_NAME);56// or connect by room instance7await client.enter(room);
Leave your PluvRoom
1import { client } from "./io";23// leave by room name4client.leave(ROOM_NAME);56// or leave by room instance7client.leave(room);