Room

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

The Room interface of the Room API is one running room: who is connected to it, the state it keeps, and the methods that send messages, remove players and schedule work. Every hook of a room definition receives it as its first argument.

Room code never constructs a Room. The room host makes one when the room is created or wakes.

Note: Room<S, P, D> takes the room’s kinds, not its schema. RoomOf<typeof schema> is the same type from the schema itself; see Room SDK.

Instance properties

Room.id
The room ID. Read only.
Room.tick
The number of ticks the room has run, kept across hibernation. Read only.
Room.tickRate
Ticks per second. Read only.
Room.connections
A read-only Map of the open connections, by connection ID.
Room.state
The shared state every connection sees.
Room.data
An object for the room’s own use. It isn’t synced and isn’t saved in room state.

Instance methods

Room.broadcast()
Sends a message to every connection.
Room.send()
Sends a message to one connection.
Room.kick()
Closes a connection with close code 4006.
Room.now()
Returns the wall-clock time in milliseconds. Under @irtio/testing it follows the test clock, unlike Date.now().
Room.setAlarm()
Sets the time at which onAlarm runs, whether the room is active or hibernated then.
Room.requestHibernate()
Asks for the room to hibernate.

Hooks

Hooks are members of the room definition, not of Room, and each is called with the Room.

onJoin()
Runs when a connection opens, including each reconnect.

Examples

Greeting a player

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

export default defineRoom({
  onJoin(room, connection) {
    room.send(connection, { type: "welcome", tick: room.tick });
    room.broadcast({ type: "joined", player: connection.player }, { except: connection });
  },
});

See also

Runtime availability

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