# RoomCloseEvent

> The RoomCloseEvent interface of the Client API is the event a RoomClient fires as close, when its connection ends for good, and as reconnecting, when it drops.

The **`RoomCloseEvent`** interface of the [Client API](/docs/client) is the event a [`RoomClient`](/docs/client/room-client) fires as its [`close` event](/docs/client/room-client/close-event), when its connection ends for good, and as its [`reconnecting` event](/docs/client/room-client/reconnecting-event), when the connection drops and the client reconnects. It extends `Event`, and has the fields of the web's `CloseEvent`, with an `error` added.

## Constructor

```ts
new RoomCloseEvent(type, code, reason, wasClean)
new RoomCloseEvent(type, code, reason, wasClean, error)
```

`type`
: `"close"` or `"reconnecting"`.

`code`, `reason`, `wasClean` and `error`
: The event's properties of the same names. `error` defaults to `undefined`.

The client creates the events it fires.

## Instance properties

All are read only. It also has `Event`'s properties, such as `type`.

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

[`RoomCloseEvent.reason`](/docs/client/room-close-event/reason)
: The close reason, or `""`.

[`RoomCloseEvent.wasClean`](/docs/client/room-close-event/wasclean)
: Whether this client closed with `close()`.

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

## Description

`RoomCloseEvent` is IRT's own class rather than a subclass of `CloseEvent`, which not every runtime has.

## Examples

### Logging every drop and close

A game sends one listener both events and tells them apart by `type`.

```ts
// connection-log.ts
import { connect, type RoomCloseEvent } from "@irtio/client";

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

function ended(event: RoomCloseEvent): void {
  const what = event.type === "close" ? "closed" : "dropped, reconnecting";
  console.warn(`${what}: ${event.code} ${event.reason}`);
}
room.addEventListener("reconnecting", ended);
room.addEventListener("close", ended);
```

## See also

- [`close` event](/docs/client/room-client/close-event)
- [`reconnecting` event](/docs/client/room-client/reconnecting-event)
- [Errors and close codes](/docs/client/errors#close-codes), for every code
