RoomClient: flush() method

Available since @irtio/client 5.0BrowsersNode

The flush() method of the RoomClient interface sends this player’s unsent owner writes at once, rather than when the client would send them.

Syntax

flush()

Parameters

None.

Return value

None (undefined).

Exceptions

None.

Description

An owner write is a write to a writable field of an entity this player owns, including its private state; see Ownership. The client batches them:

  • In a room with ticks, it sends them at most once per room tick, since the room applies them at the tick.
  • In a room without ticks, it sends them at the end of the task that made them, and a burst, such as a drag that writes on every pointer move, at most once per 16 ms. The room applies each as it arrives.

send(), each call and close() flush first, so room code applies a player’s writes before it handles what follows them. Only an unreliable message can overtake them on a real connection. Call flush() yourself when a write must reach the room without waiting for the next batch and nothing else is being sent.

While the client reconnects, writes wait in the client and flush() sends nothing; the resync after reconnecting drops them. The room’s corrections overwrite a write it didn’t take as sent, and EntityStore.onCorrect() callbacks run for the corrected fields.

Examples

Dropping a piece at once

In a board game whose room ticks, the player drags a piece they own. When they let go, the client writes the piece’s final square and sends it at once, rather than up to a tick later.

// drop-piece.ts
import { connect, defineSchema, entity } from "@irtio/client";

const Piece = entity("piece", { col: "u8", row: "u8" }, { writable: ["col", "row"] });
const schema = defineSchema({ entities: [Piece] });

const room = await connect("board-12", { schema });
const board = document.querySelector("canvas")!;

board.addEventListener("pointerup", (event) => {
  const [piece] = room.entities.mine(Piece);
  if (!piece) return;
  piece.col = Math.floor(event.offsetX / 64);
  piece.row = Math.floor(event.offsetY / 64);
  room.flush();
});

See also

Runtime availability

BrowsersNode
flush()5.05.0