loadReplay() function
Available since
@irtio/client 5.0BrowsersNodeThe loadReplay() function loads a replay or clip file and returns a Replay that shows its first tick. It’s exported by @irtio/client/replay.
Syntax
loadReplay(bytes)
loadReplay(bytes, options)
Parameters
bytes- A
Uint8Array: the replay or clip file, as the HTTP API’s download returns it. optionsOptional-
An object with this property:
schema- The game’s schema, the same
defineSchema()module the room uses. A recorded kind with the same fields as one of the schema’s is that kind, soreplay.entities.all(Ball)finds its entities. A kind that changed since the recording is read from the file;kind()returns it.
Return value
A Replay, showing its first tick.
Exceptions
Error- Thrown if
bytesisn’t a replay or clip file, if its format is one this version of@irtio/clientdoesn’t read, if it has no full state to start from, or if it declares a kind this version can’t read.
Description
The replay’s entities are in an EntityStore, with the same events as a live room’s, so the code that renders the game renders its replays too. A clip loads the same way and starts at its first second.
Your backend downloads replays and clips with its API key and decides who may watch, then serves the file to the page. @irtio/client/replay is a separate entry, so a game that doesn’t play replays doesn’t carry it.
Examples
Watching a match from your backend
A match page fetches a replay from the game’s backend and plays it at double speed.
// watch.ts
import { defineSchema, entity } from "@irtio/client";
import { loadReplay } from "@irtio/client/replay";
const Puck = entity("puck", { x: "f32", y: "f32" });
const schema = defineSchema({ entities: [Puck] });
const response = await fetch("/api/matches/42/replay");
if (!response.ok) throw new Error(`replay download failed with ${response.status}`);
const replay = loadReplay(new Uint8Array(await response.arrayBuffer()), { schema });
replay.entities.onChange(Puck, (puck) => console.log(puck.x, puck.y));
replay.speed = 2;
replay.play();
See also
Replay- Replays and clips
Room.record(), which starts a recording
Runtime availability
| Browsers | Node | |
|---|---|---|
loadReplay() | 5.0 | 5.0 |