Connection: id property

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

The id read-only property of the Connection interface identifies the connection among the room’s open connections.

Value

A number. No two open connections of the room have the same ID, and it’s the connection’s key in room.connections.

Description

The ID belongs to the connection, not to the player. A player who reconnects, after a dropped network or from a new tab, gets a new connection with a new ID. When a sleeping room wakes, each connection it held comes back with a new ID too. To keep something for a player across connections, key it by player.

Examples

Passing the turn around the table

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

export default defineRoom({
  data: () => ({ seats: [] as number[], turn: 0 }),
  onJoin(room, connection) {
    room.data.seats.push(connection.id);
  },
  onLeave(room, connection) {
    room.data.seats = room.data.seats.filter((id) => id !== connection.id);
  },
  onMessage(room, connection, message) {
    const { seats } = room.data;
    if (message !== "pass" || seats[room.data.turn % seats.length] !== connection.id) return;
    room.data.turn++;
    const next = room.connections.get(seats[room.data.turn % seats.length]!);
    if (next) room.send(next, { type: "your-turn" });
  },
});

See also

Runtime availability

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