Room: tick property

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

The tick read-only property of the Room interface is the number of ticks the room has run.

Value

A number.

  • It’s 0 in onCreate.
  • Inside the stepper and onTick, it’s the number of the tick running, 1 for a new room’s first.
  • Between ticks, in onJoin, onMessage, onLeave and onRequest, it’s the number of the last tick run. Writes made there go out with tick room.tick + 1.

Description

tick is kept in room state, so it carries on across hibernation and crashes. Ticks stop while a room is hibernated, so it doesn’t count time spent hibernated.

Note: The client’s room.tick is the tick of the last state update it applied, which is behind the room’s.

Examples

Ending a round after a number of ticks

A round that lasts 60 seconds at 20 ticks per second ends exactly 1200 ticks after it starts.

// round.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  tickRate: 20,
  data: () => ({ endsAt: 0 }),
  onCreate(room) {
    room.data.endsAt = room.tick + 1200;
  },
  onTick(room) {
    if (room.tick === room.data.endsAt) room.broadcast({ type: "round-over" });
  },
});

See also

Runtime availability

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