# RoomClient: close event

> The close event of the RoomClient interface fires when the client's connection to the room ends for good.

The **`close`** event of the [`RoomClient`](/docs/client/room-client) interface fires when the client's connection to the room ends for good, and the client doesn't reconnect.

## Syntax

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

## Event type

A [`RoomCloseEvent`](/docs/client/room-close-event). It extends `Event` and adds these properties:

[`code`](/docs/client/room-close-event/code) Read only
: The close code, or 0 when connecting failed before any close.

[`reason`](/docs/client/room-close-event/reason) Read only
: The router's or the room's close reason, or `""`.

[`wasClean`](/docs/client/room-close-event/wasclean) Read only
: `true` when this client closed with [`close()`](/docs/client/room-client/close).

[`error`](/docs/client/room-close-event/error) Read only
: The [`IrtError`](/docs/client/irt-error) that ended the connection, unless the client closed it itself.

## Description

`close` fires once, and [`readyState`](/docs/client/room-client/readystate) is `"closed"` from then on. It fires when:

- The client called `close()`. `code` is 1000, `reason` is `""`, `wasClean` is `true` and `error` is `undefined`.
- The router or the room closed the connection with a code the client doesn't retry, such as 4004 for a new build or 4011 when the room hibernated on its own request. `code` and `reason` are the close's, and `error` is named for the code.
- The first connection failed: the room couldn't be reached (`NetworkError`), `timeout` passed or an `AbortSignal.timeout()` signal aborted (`TimeoutError`), or another `signal` aborted (`AbortError`). `code` is 0. `wasClean` is `false`.

`error` is the same error [`connect()`](/docs/client/connect) and [`ready`](/docs/client/room-client/ready) reject with when the client closes before it was ready. Calls in flight reject when it closes.

A handoff that doesn't keep this client closes it with `close()` once the new room's client holds its state, so `close` fires with `wasClean` true before the [`handoff` event](/docs/client/room-client/handoff-event).

## Examples

### Telling the player why they left

A clean close goes back to the menu. A room that hibernated on its own request gets a rejoin button rather than a reconnect loop, and a new build reloads the page.

```ts
// leaving.ts
import { CloseCode, connect, reloadForNewBuild } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/dungeon-4");

room.addEventListener("close", ({ wasClean, error }) => {
  if (wasClean || !error) return showMenu();
  if (error.name === "VersionError") return reloadForNewBuild();
  if (error.code === CloseCode.RoomHibernated) return offerRejoin();
  if (error.code === CloseCode.Kicked) return showMessage(`Removed from the room: ${error.reason}`);
  console.error(error);
  showMessage(error.message);
});

declare function showMenu(): void;
declare function offerRejoin(): void;
declare function showMessage(text: string): void;
```

Branch on `error.name` or `error.code`, never on `error.message`, which is for logs.

## See also

- [`RoomCloseEvent`](/docs/client/room-close-event)
- [`RoomClient.close()`](/docs/client/room-client/close)
- [`reconnecting` event](/docs/client/room-client/reconnecting-event)
- [Errors and close codes](/docs/client/errors), for every code and error name
