Skip to content
Docs
Guides

Livestreaming

One host broadcasts, many viewers watch and chat.

A livestream is a room where a few participants publish and everyone else only watches. The split is enforced by the token, not by your UI: a viewer’s token cannot publish media, whatever the client does.

Your backend issues both tokens Host canPublish: true Room media nodes Viewers canPublish: false host token viewer token camera, mic media chat a viewer token cannot publish media, whatever the client does
Your backend issues a host token and viewer tokens. The host publishes camera and microphone to the room; viewers receive the media and can send chat messages.
import { CloudClient } from '@dtelecom/server-sdk-js';
const cloud = new CloudClient();
// Host: publishes camera, microphone and screen
function hostToken(room: string, userId: string, ip: string) {
return cloud.createToken({
room,
identity: userId,
clientIp: ip,
ttl: '12h', // cover the broadcast
webhookUrls: ['https://your-app.example/api/webhook'],
});
}
// Viewer: watches, listens and chats
function viewerToken(room: string, userId: string, ip: string) {
return cloud.createToken({
room,
identity: userId,
clientIp: ip,
grants: { canPublish: false },
webhookUrls: ['https://your-app.example/api/webhook'],
});
}

Decide the role on your backend, from your own user data or a signed host link (the example app uses a host link instead of a user database). Never let the client choose it.

A token lasts 6 hours by default and 24 hours at most. For a long broadcast, extend the host’s and the viewers’ tokens with extendToken before they expire. A participant whose token expired is removed about a minute later.

For both browser roles, return token, wsUrl and fallbackUrls from the participant’s own token response. Each viewer and host can receive a different primary node and ranked alternatives.

import { Room } from '@dtelecom/livekit-client';
async function goLive(wsUrl: string, token: string, fallbackUrls?: string[]) {
const room = new Room({ dynacast: true });
await room.connect(wsUrl, token, { fallbackUrls });
await room.localParticipant.enableCameraAndMicrophone();
return room;
}
async function shareScreen(room: Room) {
await room.localParticipant.setScreenShareEnabled(true);
}
import { Room, RoomEvent, Track } from '@dtelecom/livekit-client';
async function watch(wsUrl: string, token: string, stage: HTMLElement, fallbackUrls?: string[]) {
const room = new Room({ adaptiveStream: true });
room.on(RoomEvent.TrackSubscribed, (track) => {
if (track.kind === Track.Kind.Video || track.kind === Track.Kind.Audio) {
stage.appendChild(track.attach());
}
});
await room.connect(wsUrl, token, { fallbackUrls });
return room;
}

adaptiveStream gives each viewer the video quality that fits their player size.

Viewers can send data messages even though they cannot publish media. See Data messages and chat.

Count participant_joined and participant_left webhooks per room, or ask on demand:

import { CloudClient } from '@dtelecom/server-sdk-js';
const rooms = new CloudClient().roomService();
async function viewerCount(room: string) {
const participants = await rooms.listParticipants(room);
return participants.filter((p) => !p.permission?.canPublish).length;
}

If many clients poll the count, cache the answer for a few seconds; the example app does this.

import { CloudClient } from '@dtelecom/server-sdk-js';
const rooms = new CloudClient().roomService();
await rooms.removeParticipant('launch-stream', 'viewer-831');

Removing and muting are backend calls. When the host clicks a button in your UI, your backend checks that the user is the host and makes the call.

Sample the host’s video and audio and send them to Content moderation. One video frame every 5 seconds costs $0.006 per minute. Audio moderation adds $0.004 per minute for each participant who publishes. Viewers cost nothing to moderate.

The video rate applies only to video a participant publishes. A viewer who only watches is billed at the audio rate, whatever the quality of the stream.

Participant Per hour
Viewer, any stream quality $0.006
Host publishing audio only $0.006
Host publishing video up to 1080p $0.06
Host publishing 2K $0.12
Host publishing 4K $0.24

Example: a 1-hour 1080p stream with one host and 200 viewers costs $0.06 for the host and 200 x $0.006 = $1.20 for the viewers, about $1.26 in total.

For steady, large audiences the bandwidth add-on replaces per-minute billing with a fixed monthly price.

  • Up to 1,000 participants per room, on every plan.
  • Every viewer is a concurrent participant. The slot is taken when the token is issued, not when the viewer connects, and an unused token holds it until it expires.
  • Concurrent participants by plan: Free 100, Starter 1,000, Growth 2,500, Scale 5,000, Enterprise by agreement. The example with one host and 200 viewers is 201 participants, so it needs Starter. See Limits.

OBS can publish to a room over WHIP. Your server stands between OBS and the node:

  1. OBS sends its WHIP request to your endpoint with the stream key as its Bearer token.
  2. Your server checks the key and issues a publish-only token.
  3. Your server answers with a 307 redirect to the node’s /whip endpoint.
import express from 'express';
import { CloudClient } from '@dtelecom/server-sdk-js';
const cloud = new CloudClient();
const app = express();
// your own lookup: which room and user a stream key belongs to
declare function findStream(key: string): Promise<{ room: string; userId: string } | null>;
app.post('/whip', async (req, res) => {
const key = req.headers.authorization?.replace(/^Bearer /, '') ?? '';
const stream = await findStream(key);
if (!stream) return res.sendStatus(403);
const { token, wsUrl } = await cloud.createToken({
room: stream.room,
identity: stream.userId,
clientIp: req.ip,
grants: { canSubscribe: false }, // publish only
ttl: '12h', // must cover the whole broadcast, 24 hours at most
});
const whip = new URL(wsUrl.replace(/^ws/, 'http')); // the node's https address
whip.pathname = '/whip';
whip.searchParams.set('access_token', token);
res.redirect(307, whip.toString());
});

For a broadcast longer than the ttl, extend the token before it expires. dtelecom-live implements this flow in app/api/whip/route.ts, including the stop request (a WHIP DELETE, which gets a one-minute token) and a downloadable OBS profile for the host. Its OBS token lasts 12 hours and is not extended.

dtelecom-live is an open-source livestreaming app with host and viewer roles, chat, viewer count and OBS input. It runs on dTelecom Cloud as is: set DTELECOM_API_KEY, DTELECOM_API_SECRET and DTELECOM_CLOUD_URL, or use the Deploy to Vercel button in its README. Viewers can scan or paste a hosted Live invite in the React Native example to watch and chat on iOS or Android. A signed host invite lets the mobile app broadcast.