Typed Rooms
Typed rooms combine state management with automatic delta broadcasting at 60 FPS. Define a TypeScript interface for your room state, and PP handles the rest.
Server
interface GameState {
phase: "waiting" | "playing" | "ended";
score: Record<string, number>;
timer: number;
}
const lobby = server.room<GameState>("lobby-1", {
phase: "waiting",
score: {},
timer: 60,
}, { maxClients: 10, tickRate: 16 }); // 60 FPS
// Join clients (full state auto-synced)
server.on("connection", (client) => {
lobby.join(client); // Client gets full state immediately
});
// Update state — deltas auto-broadcast
lobby.update(s => {
s.phase = "playing";
s.score["player1"] = 10;
});
// Room events
lobby.on("join", (client) => console.log(client.id, "joined"));
lobby.on("leave", (client) => console.log(client.id, "left"));
// Lock room
lobby.lock(); // No new joins
lobby.unlock();Client
// Full state on join
client.on("roomState", ({ room, state }) => {
localState = state; // Initial sync
});
// Deltas at 60 FPS
client.on("roomDelta", ({ room, delta }) => {
// Apply only the changed fields
Object.assign(localState, delta);
render(localState);
});Next: Presence