# RoomClient: handoff event

> The handoff event of the RoomClient interface fires when a handoff connected the player to another room, or failed to.

The **`handoff`** event of the [`RoomClient`](/docs/client/room-client) interface fires when a handoff connected the player to another room, such as a match a lobby started, or failed to.

## Syntax

```ts
addEventListener("handoff", (event) => {})
```

## Event type

A `CustomEvent` whose `detail` is an object with these properties:

`room`
: A string. The new room's ID.

`url`
: A string. The new room's URL.

`type`
: A string. The new room's type.

`client`
: The `RoomClient` connected to the new room, which holds its state. Its kinds are typed `any`, since the room type is known only at runtime. Absent when the handoff failed.

`error`
: The [`IrtError`](/docs/client/irt-error) that kept the client from connecting. Absent when the handoff succeeded.

`as(schema)`
: A method that returns `client` typed for `schema`, the schema `targets` names for `type`, so its state and calls are checked. It throws an `Error` when the room's schema hash isn't `schema`'s, and throws `error` when the handoff failed.

## Description

When the router hands the player off, this client connects to the new room itself, with the same token or guest, and the schema [`connect()`](/docs/client/connect)'s `targets` option names for the room's type, or the empty schema for a type `targets` doesn't name.

Once the new client holds the room's state, this client waits up to 3 seconds for the replies to its calls in flight, closes and fires `handoff`. Its [`close` event](/docs/client/room-client/close-event) fires first, with `wasClean` true. A handoff that keeps the old room's connection, as a party's does, leaves this client open.

If the new client fails for good, `handoff` carries its `error`, and this client stays connected to the room it was in.

## Examples

### Following a lobby to its match

The lobby's client passes the match's schema in `targets`, and the handoff's client is typed with it.

```ts
// follow-match.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Race = entity("race", { lap: "u8" });
const matchSchema = defineSchema({ state: Race });

const lobby = await connect("https://rooms.irt.io/my-game/lobby", { type: "lobby", targets: { match: matchSchema } });

lobby.addEventListener("handoff", ({ detail: handoff }) => {
  if (handoff.error) {
    console.error(`couldn't join the match: ${handoff.error.message}`);
    return;
  }
  const match = handoff.as(matchSchema);
  console.log(`racing in ${handoff.room}, lap ${match.state.lap}`);
});
```

To connect to the match again later by URL, such as after a page reload, pass its type: `connect(handoff.url, { type: "match" })`.

## See also

- [`lobby` event](/docs/client/room-client/lobby-event)
- [`connect()`](/docs/client/connect), for the `targets` option
- [`Room.handoff()`](/docs/room/room/handoff), the room side
- [Lobbies and parties](/docs/room/guides/lobbies), the guide
