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.ts
2
3import { y } from "@pluv/client";
4import { z } from "zod";
5import { client } from "./io";
6
7const ROOM_NAME = "my-test-room";
8
9export const room = client.createRoom(ROOM_NAME, {
10 // Define your presence schema
11 presence: z.object({
12 selectionId: z.nullable(z.string()),
13 }),
14 // Define the user's initial presence value
15 initialPresence: {
16 selectionId: null,
17 },
18 // Define the initial storage for the room
19 initialStorage: () => ({
20 messages: y.array(["hello world!"]),
21 }),
22});

Connect to your PluvRoom

1import { client } from "./io";
2
3// connect by room name
4await client.enter(ROOM_NAME);
5
6// or connect by room instance
7await client.enter(room);

Leave your PluvRoom

1import { client } from "./io";
2
3// leave by room name
4client.leave(ROOM_NAME);
5
6// or leave by room instance
7client.leave(room);