# Watch and share a world

> Watch what a Minecraft client sees in your browser while you develop, and share the stream through a relay so anyone with the link can watch from anywhere.

The [live view](https://minekube.com/games/local-development/live-view) draws a client's world in a browser. This page shows how to watch a client on your machine, and how to share its stream.

> **Preview:**
> The camera bundle and the web app are in the [Minekube repository](https://github.com/minekube/minekube) and run from a checkout. There is no `mk` command that installs the camera yet.

## Watch a client locally

1. Start the dev server (`mk server`) and the daemon (`mk daemon`).

2. Start a client with the camera bundle, `packages/view/camera/camera.ts`, built as a client bundle. The daemon API takes it in the `bundles` record of `POST /clients`. The view's own tests do this in `packages/view/test/client.ts`:

   ```ts
   bundles[`view-${path.basename(entry, ".ts")}`] = yield* buildBundle(project, path.join(project, entry), "@minekube/client/prelude");
   const api = yield* HttpApiClient.make(Api);
   yield* api.clients.create({ payload: { name, server: "localhost:25569", bundles } });
   ```

   The camera writes the world stream to `minekube/world/stream.ndjson` in the client's game directory. The daemon serves it at `GET /clients/<name>/world`.

3. Start the web app in `apps/web`:

   ```sh
   bun run dev
   ```

   Vite forwards `/daemon` to the daemon on `127.0.0.1:4775`. No other site can talk to the daemon, because the browser reaches it through the dev server instead of CORS.

4. Open `/watch?client=<name>` on the address that Vite prints.

## Share a stream

`mk share` sends a client's world stream to a relay, where browsers anywhere can watch it:

```sh
MINEKUBE_PUBLISH_KEY=<key> mk share <client> --relay https://games.minekube.com
```

It prints the address to watch, `https://games.minekube.com/watch?stream=<client>`, and says when someone starts or stops watching.

- **The key** is the one the relay was deployed with. Only holders of the key can publish.
- **The stream flows only while someone watches.** When the first viewer arrives, the relay asks `mk share` for a snapshot of the current world, then forwards each change. When the last viewer leaves, `mk share` sends nothing but a ping now and then, to keep the connection open.
- **A viewer who joins late** gets the snapshot from the relay, which keeps it in memory.
- **Anyone with the link can watch.** Choose who may watch before you share something private.

## Run your own relay

The relay for streams is a Cloudflare Worker, `Web`, with one Durable Object `Stream` per stream. It also serves the web app. `infra/alchemy.run.ts` declares it with Alchemy:

1. Put the publish key in `infra/.env` as `MINEKUBE_PUBLISH_KEY`. The deploy binds it as a secret.
2. Run `bun run plan` in `infra` to see what changes, then `bun run deploy`. It builds `apps/web` and deploys the Worker.
3. Pass your Worker's URL to `mk share --relay`, or set `MINEKUBE_RELAY`.

`alchemy dev` in `infra` runs the same Worker locally.

## Related

- [Live view](https://minekube.com/games/local-development/live-view): how the stream and the renderer work.
- [Test with bot clients](https://minekube.com/games/testing/bots): the daemon API that starts clients.
