RoomDefinition: seesPresence() hook
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe seesPresence() hook of a room definition decides whether a connection sees another player’s presence entry, such as to hide players it blocked. Without it, every connection sees every player’s presence.
Syntax
defineRoom({
seesPresence(room, viewer, entry) {},
});
Parameters
room- The
Room. viewer- The
Connectionthat would see the entry. entry- The
PresenceEntryof another player.
Return value
true if viewer sees entry, else false.
Exceptions
If seesPresence throws, the room stops and its connections close with close code 4007, as for an error in interest.
Description
The room calls seesPresence at each state update, for each connection and each other player’s entry, so keep it cheap. A player always sees its own entry, and seesPresence isn’t called for it. A viewer sees another player’s entry only while seesPresence returns true.
Presence doesn’t follow interest: without seesPresence, every connection sees every entry, whatever interest says. An entry a connection sees counts toward the 16,384 entities it sees.
Examples
Hiding blocked players
// blocking.ts
import { defineRoom } from "@irtio/room";
export default defineRoom({
onJoin(_room, connection) {
connection.data.blocked = new Set<string>();
},
onMessage(_room, connection, message) {
const m = message as { type?: string; player?: unknown };
if (m.type === "block" && typeof m.player === "string") connection.data.blocked.add(m.player);
},
seesPresence(_room, viewer, entry) {
return !viewer.data.blocked?.has(entry.player);
},
});
See also
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
seesPresence() | 0.1 | 0.1 | 0.1 | 0.1 |