Interest

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The Interest interface of the Room API decides which entities each connection sees: a connection sees an entity when they share an interest key. A room definition’s interest is one; without it, every connection sees every entity.

Every function gets the room first, as hooks do. grid() and combine() make the functions for you.

Instance methods

Interest.keysOf(room, entity)
Returns the keys, strings or numbers, of an entity. The room calls it at the end of each tick for entities that changed.
Interest.subscriptionsOf(room, connection)
Returns the keys a connection subscribes to. The room calls it for every connection each tick.
Interest.sees(room, connection, entity) Optional
Narrows what a connection sees: an entity that shares a key with it is seen only while this returns true. It runs each tick for every such pair, before limit.

Instance properties

Interest.limit Optional
An object { max, priority(room, connection, entity) } that caps each connection’s view at the max entities with the highest priority. A visible entity stays until it ranks below max plus 10%, so entities near the cut don’t enter and leave every tick.

Description

An entity that newly shares a key with a connection enters its view with all its fields; one that no longer shares any leaves it. A connection subscribes to at most MAX_KEYS (4,096) keys and sees at most MAX_VISIBLE (16,384) entities; past either, the room logs an error and leaves the rest out. An error thrown in an interest function stops the room.

Examples

Each team sees its own units, the nearest first

// fog.ts
import { defineRoom, defineSchema, entity } from "@irtio/room";

const Unit = entity("unit", { team: "string", x: "f32", y: "f32" });
const teamOf = (meta: unknown) => (meta as { team?: string } | null)?.team ?? "neutral";

export default defineRoom({
  schema: defineSchema({ entities: [Unit] }),
  interest: {
    keysOf: (_room, e) => [`team:${e.team as string}`],
    subscriptionsOf: (_room, connection) => [`team:${teamOf(connection.meta)}`],
    limit: {
      max: 200,
      priority: (_room, connection, e) => -Math.hypot((e.x as number) - (connection.data.x ?? 0), (e.y as number) - (connection.data.y ?? 0)),
    },
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
Interest0.10.10.10.1