Client: Rooms
Rooms group connections together. Use them to organize users, broadcast messages, or track who is present in a shared context.
Joining a room
Use client.joinRoom(...)
to join a room. This also supports optional presence tracking:
// Just join
const { success, present } = await client.joinRoom("lobby");
if (success) {
console.log("Current members:", present);
}
// Join and receive presence updates
const { success, present } = await client.joinRoom("lobby", (update) => {
if (update.type === "join") {
console.log("User joined:", update.connectionId);
} else if (update.type === "leave") {
console.log("User left:", update.connectionId);
}
});
💡
The present
array always reflects current members in the room at time of
join, even if you don’t subscribe to presence updates.
Leaving a room
Use client.leaveRoom(...)
to exit:
await client.leaveRoom("lobby");
If you joined with presence tracking, it will be automatically unsubscribed.
Rejoining after reconnect
If the connection drops, you’ll need to rejoin rooms manually:
const joinedRooms = new Set(["lobby", "123"]);
client.on("reconnect", async () => {
for (const room of joinedRooms) {
await client.joinRoom(room);
}
});
Use cases
- Shared chat rooms
- Multiplayer game lobbies
- Collaborative workspaces
- Audio/video channels
Rooms are lightweight, real-time groupings. Combine with presence for full user visibility.
Last updated on