Server: Rooms
Rooms let you group connections and broadcast messages to them.
Mesh tracks room membership using Redis, so it works across all server instances and automatically cleans up when connections disconnect.
Server-side helpers
Use these methods to manage rooms manually or inside custom commands:
server.addToRoom(roomName, connection);
server.removeFromRoom(roomName, connection);
server.removeFromAllRooms(connection);
server.getAllRooms(); // string[]
const isIn = await server.isInRoom(roomName, connection); // boolean
const members = await server.getRoomMembers(roomName); // string[]
await server.clearRoom(roomName); // removes all members
💡
Most apps won’t need to use these APIs. The client SDK handles join/leave
automatically via client.joinRoom(...)
and client.leaveRoom(...)
, which
invoke the built-in "mesh/join-room"
and "mesh/leave-room"
commands.
Access control
The built-in mesh/join-room
command can be intercepted with middleware to enforce auth or role checks:
server.useMiddleware(async (ctx) => {
if (ctx.command === "mesh/join-room") {
const { roomName } = ctx.payload;
const meta = await server.connectionManager.getMetadata(ctx.connection);
if (!meta?.canJoinRooms) throw new Error("Access denied");
if (roomName.startsWith("admin:") && !meta.isAdmin) {
throw new Error("Admins only");
}
}
});
This gives you full control over who can join what.
See Client SDK → Rooms for how clients join and leave rooms.
Last updated on