RoomClient: open event

Available since @irtio/client 5.0BrowsersNode

The open event of the RoomClient interface fires when the client holds the room’s full state after having no connection: once before connect() resolves, and again after every reconnect.

Syntax

addEventListener("open", (event) => {})

Event type

A RoomOpenEvent. It extends Event and adds this property:

reconnected Read only
false for the first open, true after each reconnect.

Description

The first open fires before connect() and ready resolve, with reconnected false, so a listener added after await connect(...) sees only reconnects. After a reconnect it fires whether the client resumed or the room sent its state whole. It fires before the update event for the same state update.

Between a drop and the next open, the client fires a reconnecting event. A room that wakes from sleep fires a wake event instead, since the connection never dropped.

After a reconnect’s open, entities the room sent again are new objects: every entity after a full update, and after a resume those that changed and those the player owns. Look them up again, or rebuild from onEnter().

Examples

Telling the player they’re back

A status line set while the client reconnects clears when it holds the room’s state again. The first open has already fired when connect() resolves, so this listener sees only reconnects.

// back-online.ts
import { connect } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/harbour");
const status = document.querySelector("#status")!;

room.addEventListener("reconnecting", () => {
  status.textContent = "Reconnecting";
});
room.addEventListener("open", (event) => {
  if (!event.reconnected) return;
  status.textContent = "";
  console.log(`back after ${room.joinTime?.ms} ms`);
});

After a reconnect, joinTime is the time from the drop to the resync.

See also

Runtime availability

BrowsersNode
open event5.05.0