Room: connections property

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

The connections read-only property of the Room interface is a read-only Map of the room’s open connections, keyed by connection ID.

Value

A ReadonlyMap from connection ID to Connection.

Description

A connection is in the map from just before onJoin runs, so onJoin sees it, until it closes: it’s gone from the map by the time onLeave runs for it. A player has at most one connection; when a player connects again, the older connection closes first.

Connections aren’t room state. A room that hibernates closes them, so after a wake the map fills as players reconnect. A room that went to sleep is the exception: its connections are back in the map in onWake, with their data.

Bots have no connection, so they aren’t in the map; room.presence lists people and bots.

Examples

Taking a room off quick join once it’s full

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

export default defineRoom({
  onJoin(room, connection) {
    if (room.connections.size > 2) return room.kick(connection, "the duel is full");
    if (room.connections.size === 2) room.setPublic(false);
  },
  onLeave(room) {
    if (room.connections.size < 2) room.setPublic(true);
  },
});

See also

Runtime availability

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