loadClip() function
@irtio/client 5.0BrowsersNodeThe loadClip() function downloads a clip its room made with share: true and returns a Replay that shows its first tick. It’s exported by @irtio/client/replay.
Syntax
loadClip(source)
loadClip(source, options)
Parameters
source-
An object with these properties:
room- A string or
URL: the URL of the room that made the clip, or one relative to the project URL, such as the room’s ID. projectOptional- A string or
URL: the project URL a relativeroomresolves against. Defaults to the page’sirt-projectmeta tag. clip- A string: the clip’s ID, as
room.clip()returned it. tokenOptional- A string: a player token. Leave it out to download as a guest, which needs the project’s guests setting.
signalOptional- An
AbortSignalthat aborts the download.
optionsOptional- An object with the option
loadReplay()takes,schema.
Return value
A Promise that resolves with a Replay, showing the clip’s first tick.
Exceptions
TypeError- Rejects with this if
roomis relative and there’s no project URL to resolve it against; if its path doesn’t end in a project and a room ID; or if it, or the project URL, isn’t anhttp:,https:,ws:orwss:URL. IrtError- Rejects with this if the download fails, with the HTTP status as its
code:404for a clip that doesn’t exist or isn’t shared, and429past 30 downloads a minute from one address. Rejects with one of code0too: namedNetworkErrorwhen the router can’t be reached, andAbortErrorwhensignalaborts, orTimeoutErrorwhen it’s fromAbortSignal.timeout(), with the signal’s reason as itscause. Error- Rejects with this if the file isn’t one this version of
@irtio/clientcan read, asloadReplay()throws.
Description
A clip is written a moment after room.clip() returns, so a player told of it at once may ask first. loadClip() tries a 404 again after 0.25, 0.5, 1 and 2 seconds before it rejects.
Anyone who can get a token for the project and knows the room and clip IDs can download a shared clip. A clip of a recording with private: true can’t be shared. Sharing clips with players covers the room’s side.
Examples
Playing the final round’s clip
The room broadcasts the clip’s ID and room when a match ends, and each player downloads it and plays it.
// final-clip.ts
import { connect, defineSchema, entity } from "@irtio/client";
import { loadClip } from "@irtio/client/replay";
const Ball = entity("ball", { x: "f32", y: "f32" });
const schema = defineSchema({ entities: [Ball] });
const room = await connect("match-42", { schema });
room.addEventListener("message", async ({ data }) => {
const message = data as { type?: string; room?: string; clip?: string };
if (message.type !== "clip" || !message.room || !message.clip) return;
try {
const replay = await loadClip({ room: message.room, clip: message.clip }, { schema });
replay.entities.onChange(Ball, (ball) => console.log(`ball at ${ball.x}, ${ball.y}`));
replay.play();
} catch (error) {
console.error("couldn't load the clip", error);
}
});
The broadcast names the room because players often watch from another one, such as the lobby they go back to after the match.
See also
loadReplay()ReplayRoom.clip(), which makes the clip- Replays and clips
Runtime availability
| Browsers | Node | |
|---|---|---|
loadClip() | 5.0 | 5.0 |