Playground key
create a key →

Guide

Recipe: audience-controlled robot.

A Twitch-style robot anyone can queue up to drive. The queue is yours — Redis, Postgres, a Discord bot, whatever. DroidHub only needs to be told who's up. The key for this service needs tokens:mint and controller:write.

1 — Backend: viewers join, queue decides, control rotates

server.ts
const API = "https://platform.droidhub.cloud/api";
const headers = {
  Authorization: `Bearer ${process.env.DROIDHUB_API_KEY}`,
  "Content-Type": "application/json",
};

// Every spectator gets a viewer token (watch-only). One identity per
// connection: a second connection with the same identity replaces the first.
async function joinAsViewer(streamId, userId) {
  const res = await fetch(`${API}/v1/streams/${streamId}/tokens`, {
    method: "POST",
    headers,
    body: JSON.stringify({ identity: userId, role: "viewer" }),
  });
  return res.json(); // { token, url } -> send to the user's browser
}

// When your queue pops, promote the next user. The previous
// controller is revoked server-side in the same call.
async function giveControl(streamId, userId) {
  await fetch(`${API}/v1/streams/${streamId}/controller`, {
    method: "POST",
    headers,
    body: JSON.stringify({ identity: userId }),
  });
}

// Turn over: revoke only if it's still theirs.
async function endTurn(streamId, userId) {
  await fetch(`${API}/v1/streams/${streamId}/controller?identity=${userId}`, {
    method: "DELETE",
    headers,
  });
}

Mint viewer tokens and promote one identity at a time. Connected viewers upgrade to control live, and a reload keeps control. When a driver disconnects for good, control is revoked and a controller.revoked webhook with reason disconnected tells your queue to move on.

2 — Frontend: watch + drive with livekit-client

client.ts
import { Room, RoomEvent } from "livekit-client";

const room = new Room();
await room.connect(url, token); // from /v1/streams/:id/tokens

// Render the robot's camera (~200 ms end to end).
room.on(RoomEvent.TrackSubscribed, (track) => {
  if (track.kind === "video") el.appendChild(track.attach());
});

// Fires when YOUR backend promotes this user — flip the UI to gamepad mode.
room.on(RoomEvent.ParticipantPermissionsChanged, () => {
  setCanDrive(room.localParticipant.permissions?.canPublishData ?? false);
});

// Discrete commands: RPC to the robot, which registers the method and
// answers. Its identity is robot_<streamId> unless it picked its own.
await room.localParticipant.performRpc({
  destinationIdentity: `robot_${streamId}`,
  method: "control",
  payload: JSON.stringify({ cmd: "honk" }),
});

// Joystick axes: [left, right], -100..100, about 20 times a second while
// input is held, then a few zero frames. Lossy: only the newest matters.
// Same format as the dashboard's drive pad.
await room.localParticipant.publishData(
  new TextEncoder().encode(JSON.stringify([left, right])),
  { reliable: false },
);

The robot side of this is in Hardware & devices.

3 — The crowd: HLS for everyone else

Realtime WebRTC viewers cost SFU capacity; the live HLS playlist (hlsPlaybackUrl on the stream, about 6 s behind, CDN-cached) scales to any audience for free. A common split: the current controller and the on-deck queue watch over WebRTC, everyone else gets HLS.

Reacting to liveness

Subscribe to webhooks (stream.live / stream.ended) to open and close your queue automatically as the camera comes and goes.