# RoomDefinition: onJoin() hook

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

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

## Syntax

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

### Parameters

`room`
: The [`Room`](/docs/room/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

```ts
// 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](/docs/room/sdk#hooks), for when every hook runs and what happens if it throws
- [Presence](/docs/room/presence)
