RoomDefinition: onJoin() hook
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe onJoin() hook of a room definition runs when a connection opens, including each reconnect.
Syntax
defineRoom({
onJoin(room, connection) {},
});
Parameters
room- The
Roomthe 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
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
onJoin() | 0.1 | 0.1 | 0.1 | 0.1 |