RoomClient: reconnecting event

Available since @irtio/client 5.0BrowsersNode

The reconnecting event of the RoomClient interface fires when the connection drops and the client starts reconnecting by itself.

Syntax

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

Event type

A RoomCloseEvent. It extends Event and adds these properties:

code Read only
The close code the connection dropped with.
reason Read only
The close reason it dropped with.
wasClean Read only
Always false on reconnecting.
error Read only
Always undefined on reconnecting.

Description

The client reconnects after a drop with a close code it retries; Errors and close codes lists which. After 4008, when the room moved or restarted, its first attempt is at once; otherwise it tries again after a backoff of 250 ms doubling up to 10 s, with jitter. reconnecting fires once per drop, not once per attempt. A drop with a code the client doesn’t retry fires a close event instead.

When it fires, readyState is "reconnecting", or "connecting" if the client hadn’t been ready yet. Calls in flight have rejected with an IrtError named NetworkError, and messages sent until the client is back are dropped. asleep is false again.

The client is back when an open event fires with reconnected true.

Examples

Pausing input while reconnecting

A racing game stops sending steering while the client reconnects, and logs the code for the player’s bug report.

// steering.ts
import { CloseCode, connect } from "@irtio/client";

const room = await connect("https://rooms.irt.io/my-game/track-3");
let steering = true;

room.addEventListener("reconnecting", (event) => {
  steering = false;
  const why = event.code === CloseCode.RoomMoved ? "the room moved" : event.reason;
  console.warn(`connection dropped with ${event.code} (${why}), reconnecting`);
});
room.addEventListener("open", () => {
  steering = true;
});

document.addEventListener("keydown", (key) => {
  if (steering) room.send({ type: "steer", key: key.code });
});

See also

Runtime availability

BrowsersNode
reconnecting event5.05.0