RoomDefinition: onJoin() hook

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

The onJoin() hook of a room definition runs when a connection opens, including each reconnect.

Syntax

defineRoom({
  onJoin(room, connection) {},
});

Parameters

room
The Room the connection joined.
connection
The connection that opened.

Return value

None, or a Promise. A joining connection gets the full state as soon as onJoin has run, or once the promise it returned settles.

Exceptions

If onJoin throws, or its promise rejects, that connection closes with close code 4006. The room keeps running.

Description

A joining connection doesn’t wait for a tick, so join time is the round trip plus onJoin, whatever the tick rate.

Warning: After a room host crash, onJoin runs again for every player who reconnects, with no onLeave before it, while their entities from before still exist. A room that creates a player’s entity here should reuse the one already there.

Examples

Telling the room who arrived

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

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

See also

  • Hooks, for when every hook runs and what happens if it throws
  • Presence

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
onJoin()0.10.10.10.1