# loadClip() function

> The loadClip() function of @irtio/client/replay downloads a clip its room shared and loads it for playing back, at its first tick.

The **`loadClip()`** function downloads a clip its room made with `share: true` and returns a [`Replay`](/docs/client/replay) that shows its first tick. It's exported by `@irtio/client/replay`.

## Syntax

```ts
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.

  `project` Optional
  : A string or `URL`: the project URL a relative `room` resolves against. Defaults to the page's `irt-project` meta tag.

  `clip`
  : A string: the clip's ID, as [`room.clip()`](/docs/room/room/clip) returned it.

  `token` Optional
  : A string: a player token. Leave it out to download as a guest, which needs the project's guests setting.

  `signal` Optional
  : An `AbortSignal` that aborts the download.

`options` Optional
: An object with the option [`loadReplay()`](/docs/client/loadreplay) takes, `schema`.

### Return value

A `Promise` that resolves with a [`Replay`](/docs/client/replay), showing the clip's first tick.

### Exceptions

`TypeError`
: Rejects with this if `room` is 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 an `http:`, `https:`, `ws:` or `wss:` URL.

[`IrtError`](/docs/client/irt-error)
: Rejects with this if the download fails, with the HTTP status as its `code`: `404` for a clip that doesn't exist or isn't shared, and `429` past 30 downloads a minute from one address. Rejects with one of code `0` too: named `NetworkError` when the router can't be reached, and `AbortError` when `signal` aborts, or `TimeoutError` when it's from `AbortSignal.timeout()`, with the signal's reason as its `cause`.

`Error`
: Rejects with this if the file isn't one this version of `@irtio/client` can read, as [`loadReplay()`](/docs/client/loadreplay) 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](/docs/room/guides/replays#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.

```ts
// 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()`](/docs/client/loadreplay)
- [`Replay`](/docs/client/replay)
- [`Room.clip()`](/docs/room/room/clip), which makes the clip
- [Replays and clips](/docs/room/guides/replays#sharing-clips-with-players)
