Skip to main content

How calls and live streams work

Markdown

How a call, a group call and a live stream start, who gets in, how you run them while they last, and how they end. Every use case builds on this page.

In your stack: calls in Next.js · Express · Laravel · PHP · Django · FastAPI · Flask · WordPress, and live streams in Next.js · Express · Laravel · PHP · Django · FastAPI · Flask · WordPress

The code of the page here is that of the browser, with React beside it where React differs. In React, the same calls and streams come from hooks, useCall(), useGroupCall( name, getToken ) and useStream( name, getToken ), and do the same from there (React).

Nothing to create or delete​

A call, a group call and a live stream each have a name your app picks, such as team:7 or launch:12. There is nothing to create before and nothing to delete after:

  • It starts when a page uses a token your server made for it.
  • It ends when its people leave, or when your server or its host ends it.
  • Your app decides who gets in and what they may do, in a route of its own: it makes your own check, then answers a token. The SDK holds none of that logic.
Starts whenWho gets inWho runs itEnds when
A call between two peoplea page calls someone with a token from your routethe caller, with your token, and the person it rings, who answers with no token of their ownthe two peopleone of them hangs up, or nobody answers within 45 seconds
A group callthe first person joinseach person your route gives a token, with what it says they may doyour server, and its hostyour server ends it, or the last person leaves
A live streamits host opens it, then goes liveeach person your route gives a token, which says who they are in itits host, those who keep its chat, and your serverits host or your server ends it, and it may go live again

How a page asks your server​

Your app decides in a route of its own, the way it answers any other request of its pages. Say Ben, user 42, calls Ann, user 34, as in calls from a button, which builds it step by step. WordPlus Cloud is not asked first. It goes like this:

  1. The page of Ben asks your route for a token, with a GET to an address of your own, such as /api/call-token?to=34&type=video. It carries his usual cookie, as every request from your page does.
  2. Your route makes your own check. It finds who is asking the way your app does (your session, or auth() in Laravel), checks whatever your app needs, then answers realtime.callToken( me, to, { type } ): a token your server signs with your secret, for this call alone. A no is a 401 or 403, the page hears forbidden, and nothing rings.
  3. The page hands the token to WordPlus Cloud, with rt.startCall( token ) or the ready-made button, and WordPlus Cloud checks the signature and rings Ann.
  4. Ann answers with no token of her own: the token that rang her proves your server said yes. Her page answers through the routes of the package, which you added once when you set it up (getting started): POST /api/realtime/calls, /api/realtime unless you choose another.
page of Ben      →  your route        GET /api/call-token?to=34&type=video
your route → page of Ben your check says yes: a token signed with your secret
page of Ben → WordPlus Cloud the token
WordPlus Cloud → pages of Ann ring

WordPlus Cloud does not ask your server again: it trusts only what your secret signed, so a page can neither skip your route nor change what it answered. A group call and a live stream start the same way, each from a route of yours: groupCallToken() and streamToken(). The package routes then carry the rest: the line where calls ring, answering, a token renewed, and the acts of a host, on proofs only your server makes. Rooms ask a rule of your server instead, at /api/realtime/auth (rooms).

What your route answers:

  • A 401 or 403 keeps them out, with the error forbidden on the page.
  • A token lets them in, with what its options say, such as publish: false for someone who listens in a group call, or a role in a live stream.
  • A token is for its user, and on anyone else's page it starts nothing. A call token rings once and lasts 6 hours at most, so make it as the person asks. A stream token lasts ten minutes, and the page asks your route again as each runs out, so a change in your data counts by then. To take someone out at once, see taking access away.

Your server or ours​

Only the last two lines of the picture above reach WordPlus Cloud. The request to your token route goes to your own website, like a request to any other route of yours: WordPlus Cloud never sees it, your checks or your sessions. It sees only the token your server signed.

The requestGoes toAnswered by
your token routes, such as GET /api/call-token, from your pagesyour serveryour own code, then the WordPlus package, which signs the token
POST /api/realtime/calls, /auth and /keys, from your pagesyour serverthe WordPlus package on it: answering and renewing calls, the acts of a host, and the rules of your rooms
the signed token, then the call or the stream itselfWordPlus CloudWordPlus Cloud
a webhook such as call.ended, from WordPlus Cloudyour server, at /api/realtime/webhookthe WordPlus package, then your on() handlers

Your server takes no part in the call itself. Its sound and video never pass through it.

Any address you like​

/api/realtime is only where the routes go unless you say otherwise. Move them all under another start with base, or give each route an address of its own with routes. They stay on the same website as your pages, since they answer no other site.

The page needs no change either way. Your server writes the addresses of the routes into the settings of the page (realtime.config( user )), and the SDK in the page sends its requests where they say.

All of them under another start​

Such as /my/own/address/auth, /my/own/address/calls and so on. Set the start in the two places that must match: where you add the routes, and the base option.

const realtime = createRealtime( {
// Your keys, as before, then the address of the routes.
base: '/my/own/address',
} );

app.use( '/my/own/address', realtime.node( ( req ) => req.session.user ?? null ) );

Each at an address of its own​

Such as /check-call for the calls route. Name each address in routes, and the package answers each route there alone. A route you leave out stays at /api/realtime/{name}, or under your base.

const realtime = createRealtime( {
// Your keys, as before, then each route where you want it.
routes: {
auth: '/permit/rooms',
calls: '/check-call',
keys: '/my/keys',
webhook: '/hooks/wordplus',
},
} );

// Once for the whole app: it answers those four addresses and hands every other request on.
app.use( realtime.node( ( req ) => req.session.user ?? null ) );

// Next.js: a file for each address, such as app/check-call/route.ts
export const POST = realtime.route;

Moved the webhook, with base or in routes? Then also change the webhook address on your app page in your WordPlus Cloud account, such as https://your-site.com/my/own/address/webhook or https://your-site.com/hooks/wordplus. A phone app has no website of its own, so it names the full addresses: endpoint, such as https://your-site.com/my/own/address, and in routes any route at an address of its own. Made from your server config(), it has them all already.

Calls between two people​

Starting one​

The page asks your route for a token, then calls with it. Here Ben calls Ann, user 34:

// Asks your route for a token: GET /api/call-token, under Who may call below.
async function callToken( to ) {
const answer = await fetch( '/api/call-token?type=video&to=' + to );
if ( ! answer.ok ) throw new Error( 'Your server said no' );

return ( await answer.json() ).token;
}

const call = await rt.startCall( await callToken( 34 ) );

Or a ready-made button, with the call screen once on the page: <call-button to="34" token-url="/api/call-token" type="video">Call support</call-button> and await rt.callScreen(), or in React <CallButton to={ 34 } tokenUrl="/api/call-token"> and <CallScreen />. A press asks your route with ?to=34&type=video.

It rings on every page and app user 34 has open with the call screen or rt.onIncomingCall(), and stops ringing everywhere once one answers. Answering takes no token:

await rt.onIncomingCall( async ( incoming ) => {
const call = await incoming.accept(); // or incoming.decline()
} );

Who may call​

Your route decides, before anything rings. This one lets a customer call an agent, and nobody else, and isAgent() is yours (calls from a button):

// Your route, GET /api/call-token?to=34&type=video. Express here, the same in any framework.
app.get( '/api/call-token', async ( req, res ) => {
const me = userOf( req );
const { to, type } = req.query;

// Your own check: one of the two is an agent.
if ( ! me || ( ! ( await isAgent( me ) ) && ! ( await isAgent( to ) ) ) ) {
return res.sendStatus( 403 );
}

res.json( { token: await realtime.callToken( me, to, { type } ) } );
} );
  • name gives the call a name of your own, such as support:17, which the webhooks give back. into rings someone into a call that is up, which goes on as a group call (adding people).
  • On WordPress, its package decides by a rule of its own (WordPress calls).
  • Apps with rules keep them: by a rule.

How it connects​

A call between two people always connects, whatever the network, and changes its path on its own, without hanging up:

PathWhen
1. Directly between the two deviceson most networks. The fastest: the picture and the sound go from one device to the other
2. Through our relayswhen a firewall or a network blocks the direct path. The relays answer on the ports of the web, 443 and 80, so a call gets through wherever websites do
3. Through our media serverswhen even the relays cannot carry it. The call moves into a room on our media servers, and goes on
  • Encrypted end to end on every path, with a key only your server gives out: our relays and media servers carry what they cannot read.
  • No credits on any path. Calls between two people are in every plan, relayed or not.
  • A phone that moves from Wi-Fi to mobile data finds a new path, and the call goes on.

Ending it​

call.end();                            // hang up
call.on( 'state', ( state ) => {} ); // ringing, connecting, connected … ended
call.endReason; // why: hangup, rejected, no_answer …

It also ends on its own when nobody answers within 45 seconds, when a page closes, or when a connection is gone for more than 45 seconds (why a call ended).

From your server: a call between two people runs between the two people, so your server neither lists it nor ends it. To stop someone calling, your route answers no to their next token, and realtime.user( id ).disconnect() takes them out of the call they are in now.

Group calls​

Starting one​

There is nothing to create: the first person who joins a name opens the call, and everyone else joins the same name, each with a token from your route. Here it is the meeting of team 7:

// Asks your route for a token: GET /api/meeting-token, under Who may join below.
async function meetingToken( id ) {
const answer = await fetch( '/api/meeting-token?id=' + id );
if ( ! answer.ok ) throw new Error( 'Your server said no' );

return ( await answer.json() ).token;
}

const meeting = await rt.joinGroupCall( await meetingToken( 7 ) );

Or a ready-made button: <group-call-button call="team:7" token-url="/api/meeting-token?id=7" type="video" title="Design team">Join the meeting</group-call-button>, <GroupCallButton call="team:7" tokenUrl="/api/meeting-token?id=7"> in React. A press asks your route with ?id=7&call=team:7&type=video.

To bring people in, show the button on their page, or tell them from your server, and their page joins:

await realtime.toUser( [ 43, 56 ] ).emit( 'meeting', { id: 7 } );

The page of each of them joins when told, with a token of their own:

rt.on( 'meeting', async ( { id } ) => showMeeting( await rt.joinGroupCall( await meetingToken( id ) ) ) );

Who may join​

Your route, each time someone joins. It builds the name from the id, checks, and says in the token what this person may do. isMemberOf() and isLeadOf() are yours:

// Your route, GET /api/meeting-token?id=7. Express here, the same in any framework.
app.get( '/api/meeting-token', async ( req, res ) => {
const me = userOf( req );
const { id } = req.query;

// Your own check: only its team.
if ( ! me || ! await isMemberOf( id, me ) ) {
return res.sendStatus( 403 );
}

// What they may do in it: its lead hosts.
const token = await realtime.groupCallToken( me, 'team:' + id, {
admin: await isLeadOf( id, me ),
encrypt: true,
} );

res.json( { token } );
} );

What the token may say:

The token saysThey
nothing morejoin, send their camera, microphone and screen, and see and hear everyone
publish: falsejoin as a listener: they see and hear, and send nothing
admin: truejoin as its host: they may mute the others, let them speak or make them listeners, and take them out
hidden: truejoin unseen, listed to nobody
type: 'audio'join an audio call
  • Encrypted end to end when its tokens say encrypt: true, in every token for the call: every frame is encrypted in the browser with a key your server makes and gives only to those it let in, and the media servers forward what they cannot read (team meetings). Without encrypt, its media is encrypted on the way, and not end to end.
  • A token is for its user, and lasts 6 hours at most. Someone removed from the call needs a new one to come back, so your route is asked again.
  • On WordPress, its package decides by a rule of its own (WordPress calls).

Running it​

On the page, each person sends what they choose and leaves when they want:

await meeting.setCamera( true );   // false stops it
meeting.leave();

Everything else is for your server, with no page open:

const meeting = realtime.call( 'team:7' );
await meeting.get(); // who is in it, and what each sends
await meeting.permit( 43, { publish: true } ); // let 43 speak, and false makes them a listener again
await meeting.mute( 43, 'microphone' ); // a microphone left on
await meeting.remove( 43 ); // out now
await realtime.listCalls(); // every group call going on

Buttons for the host​

The ready-made call screen has them already, for the host your token made admin: Manage Participant on the tile of each of the others opens Mute Microphone, Stop Camera & Mic and Allow Camera & Mic (Demote to Viewer and Promote to Speaker in an audio call), and Remove from Call. Each goes to the calls route of the package, which acts only on the proof of a host that your server put in the token, so there is nothing to add. On a call screen of your own, group.mute(), group.letSpeak(), group.makeListener() and group.remove() do the same (group calls).

Buttons of your own elsewhere, such as a host panel beside the call, call a route of yours, which checks who asks and does it:

app.post( '/meetings/:id/remove', async ( req, res ) => {
if ( !( await isLeadOf( req.params.id, req.user.id ) ) ) return res.sendStatus( 403 ); // only the lead
await realtime.call( 'team:' + req.params.id ).remove( req.body.user );
res.sendStatus( 204 );
} );
removeButton.onclick = () => fetch( '/meetings/7/remove', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify( { user: 43 } ),
} );

Letting someone speak, muting and ending work the same way (classes).

Ending it​

It closes on its own once the last person leaves. Your server ends it for everyone at once:

await realtime.call( 'team:7' ).end();

Everyone is out, and the tokens signed before do not open it again. The same name opens a new call later, for whom your route lets in.

Live streams​

Starting one​

The host opens the stream with a token from your route, sees their camera, then goes live. Here it is stream 12, launch:12. A stream token lasts ten minutes, so the page gives a function that asks your route, and it is asked again as each runs out:

// Asks your route for a token: GET /api/stream-token, under Who is who below.
async function streamToken() {
const answer = await fetch( '/api/stream-token?id=12' );
if ( ! answer.ok ) throw new Error( 'Your server said no' );

return ( await answer.json() ).token;
}

const show = await rt.joinStream( streamToken );
await show.preview(); // the camera and microphone, for the host alone
await show.goLive(); // everyone watching sees it now

Or ready-made: <live-stream name="launch:12" token-url="/api/stream-token?id=12" title="Opening night"></live-stream> is the player for viewers, and for the host the studio, with Go live and End stream, <LiveStream name="launch:12" tokenUrl="/api/stream-token?id=12"> in React. It asks your route with ?id=12&stream=launch:12. From OBS, const { url, key } = await show.ingest().

Viewers open the same stream the same way, each with a token of their own: the same element, or rt.joinStream( streamToken ) and show.watch( video ), useStream( 'launch:12', streamToken ) in React. Before the host goes live, they see it waiting.

A stream is for everyone watching it, so its picture and chat are not encrypted end to end. They are encrypted on the way, and the media servers of WordPlus Cloud see them, as a stream needs (live streams).

Who is who​

Your route says, in the token it answers. Here the presenter of the stream hosts, a visitor who is not signed in reads the chat, and everyone else watches and writes. streamOf() is yours:

// Your route, GET /api/stream-token?id=12. Express here, the same in any framework.
app.get( '/api/stream-token', async ( req, res ) => {
const me = userOf( req ); // null for a visitor who is not signed in

// Your own check: a stream of your app.
const stream = await streamOf( req.query.id );
if ( ! stream ) return res.sendStatus( 403 );

// Its presenter hosts, a visitor reads the chat, everyone else watches and writes.
const place = me === stream.presenter ? { role: 'host' as const } : me === null ? { chat: 'read' as const } : {};
res.json( { token: await realtime.streamToken( me, 'launch:' + stream.id, place ) } );
} );

What the token may say:

The token saysThey
nothing morewatch, write in the chat and raise a hand
chat: 'read'watch and read the chat, without writing
mod: truewatch and keep the chat
role: 'host'go live, bring people on stage, keep the chat and end the stream
  • A visitor who is not signed in is null, and may only be a viewer: they sign in to write or raise a hand. To keep visitors out, your route answers 401 for them (members-only streams).
  • On WordPress, its package decides by a rule of its own (WordPress streams).

Running it​

The host and those who keep the chat, on the page:

await show.chat.delete( id );
await show.chat.pin( id );
await show.chat.timeout( user, 600 ); // no writing for 10 minutes
await show.remove( user ); // out now, and they may come back
await show.ban( user ); // out, and kept out

Your server, with no page open:

const show = realtime.stream( 'launch:12' );
await show.get(); // live or not, and how many watch
await show.say( 'We start at 8 pm', { name: 'The team' } );
await show.ban( 88 );
await realtime.listStreams(); // every stream live now

Your server hears each stream go live and end, from the webhook: stream.live and stream.ended (live streams).

Ending it​

The host ends it with End stream, or await show.end(). Your server ends it too:

await realtime.stream( 'launch:12' ).end();

The host may take it live again later under the same name. Its chat stays for a day after its last message, unless chat: { fresh: true } empties it each time it goes live.

Taking access away​

ToYour server
keep someone out from now onchanges your data: your route answers no when they next ask for a token, a stream asking again within ten minutes
take someone out of a group call nowrealtime.call( name ).remove( user )
take someone out of a live stream nowrealtime.stream( name ).remove( user ), or ban( user ) to keep them out
take someone out of a room nowrealtime.room( room ).remove( user ), and your rule saying no for them keeps them out
take a user out of everything nowrealtime.user( id ).disconnect(): every room, call, group call and live stream, on every device, and their pages ask your routes again

More in permissions.