# cellKey() function

> The cellKey() function returns the interest key grid() gives a cell, so room code can subscribe to cells of its own choosing.

The **`cellKey()`** function returns the interest key [`grid()`](/docs/room/grid) gives a cell: the cell coordinates joined by commas.

## Syntax

```ts
cellKey(x, y)
cellKey(x, y, z)
```

### Parameters

`x`, `y`, `z`
: The cell coordinates, each `Math.floor(position / cellSize)` on its axis.

### Return value

A string such as `"3,-1"` or `"3,-1,0"`. It doesn't include a grid's `prefix`.

## Examples

### Letting spectators watch a chosen cell

A spectator subscribes to the cell it picked as well as the cells the grid gives it, so it sees the entities in both.

```ts
// spectate.ts
import { cellKey, defineRoom, defineSchema, entity, grid } from "@irtio/room";

const Unit = entity("unit", { x: "f32", y: "f32" });
const CELL = 64;
const area = grid({ cellSize: CELL, radius: 1, dims: "xy", around: (_room, c) => c.data.at });

export default defineRoom({
  schema: defineSchema({ entities: [Unit] }),
  interest: {
    keysOf: area.keysOf,
    subscriptionsOf(room, c) {
      const keys = [...area.subscriptionsOf(room, c)];
      if (c.data.watch) keys.push(cellKey(c.data.watch[0], c.data.watch[1]));
      return keys;
    },
  },
  onMessage(_room, connection, message) {
    const m = message as { watch?: [number, number] };
    if (m.watch) connection.data.watch = [Math.floor(m.watch[0] / CELL), Math.floor(m.watch[1] / CELL)];
  },
});
```

## See also

- [`grid()`](/docs/room/grid)
- [The grid](/docs/room/guides/state-sync#the-grid)
