# Interest

> The Interest interface of the Room API decides which entities each connection sees, by the keys they share.

The **`Interest`** interface of the [Room API](/docs/room) 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()`](/docs/room/grid) and [`combine()`](/docs/room/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

```ts
// 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

- [Interest](/docs/room/guides/state-sync#interest), in the state sync guide
- [`grid()`](/docs/room/grid)
- [`combine()`](/docs/room/combine)
