Skip to content
Get started

Watch and share a world

View as Markdown

The 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.

  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:

    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:

    Terminal window
    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.

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

Terminal window
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.

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.

Was this helpful?