Skip to content
Beam Docs
Media

Quickstart: Stage

A few speakers and a large audience over WebRTC: listener tokens, promotion to speaker, relays near listeners and an optional HLS output.

A stage is a call where most participants only listen: a town hall, a community event, a podcast recorded live. Speakers publish to an SFU; Beam adds relay Workers in the listeners' regions as the audience grows, so latency stays under half a second for everyone. This guide reuses the backend of the call quickstart (steps 1 and 2).


1. Create the session

beam media session create --project mprj_... --profile stage --name "Town hall"

A stage is public by default: Workers can read the media, which allows an HLS output for very large audiences. Pass --protection e2ee to encrypt it end to end instead (an end-to-end encrypted stage has no HLS output).


2. Tokens: speakers and listeners

Speakers get a publish token, listeners a subscribe token. Add to your backend:

app.post("/api/stage/token", async (req, res) => {
  const speaker = isSpeaker(req);  // your own rule: host, invited guest, …
  const { token } = await signer.mint({
    session: SESSION, role: speaker ? "publish" : "subscribe",
    identity: req.user.id, name: req.user.name, ttlSeconds: 3600,
  });
  res.json({ token, directoryUrl: process.env.BEAM_MEDIA_DIRECTORY_URL });
});

Listeners are not announced to other participants: everyone sees a listener count instead (listeners.changed in the SDK), so a stage with tens of thousands of listeners does not flood every page with join events. They are still participants for your backend: they appear in GET /v1/media/sessions/{id}/participants and in webhooks, and you can remove them.


3. The page

import { Stage } from "@beam-network/web-sdk/media";

const { token, directoryUrl } = await fetch("/api/stage/token", { method: "POST" }).then((r) => r.json());
const stage = await Stage.join(token, { directoryUrl });

stage.on("track.subscribed", (track) => {
  const element = track.attach(); // <video> or <audio>
  element.id = track.id;
  document.querySelector("#speakers")!.append(element);
});
stage.on("track.unsubscribed", (track) => document.getElementById(track.id)?.remove());
stage.on("listeners.changed", ({ count }) => {
  document.querySelector("#audience")!.textContent = `${count} listening`;
});

if (stage.canPublish) {
  await stage.publishCamera();
  await stage.publishMicrophone();
}

4. Bring a listener on stage

To let a listener speak (a raised hand your app approved), mint a publish token for the same identity and hand it to the page:

const { token: publishToken } = await fetch("/api/stage/promote", { method: "POST" }).then((r) => r.json());
await stage.promote(publishToken);
await stage.publishMicrophone();

The listener keeps its connection and stays the same participant: other pages receive participant.updated with its new role, then its tracks.

To take it off stage, kick it (POST /v1/media/sessions/{id}/kick). A kick denies the identity until the kicked token's exp, so re-admitting it as a listener needs either a new identity (for example user-42-listener-2), or a wait until that token has expired. Short speaker tokens keep that wait short.


5. Larger audiences: HLS output

Above what WebRTC should serve, publish the stage as LL-HLS through Beam's CDN (2 to 4 seconds of latency):

beam media session create --project mprj_... --profile stage --hls-output

CDN viewers get a subscribe token and play with the broadcast player, Player.load(token, { directoryUrl, video, hls: Hls }), which joins the directory with transport: "hls" and gets HLS edges. Do not use connect or Stage.join for them: they join with transport: "auto", and for a stage the directory answers WebRTC endpoints. The HLS program mixes every speaker's audio and shows the screen share, else the active speaker's camera.

Watch it from a terminal

beam media session topology msn_... --watch   # SFU, relays per region, their state
beam media session stats msn_...              # participants, viewers by region, QoE

Decentralized distributed bandwidth infrastructure.

On this page