Skip to main content

Live streams

Markdown

One person presents live to everyone watching: the picture, a chat beside it, reactions, and the count of who watches. The host goes live from the browser or streams from OBS, and may bring viewers on stage beside them. A live stream runs on the media servers of WordPlus Cloud, for a site whose WordPlus Cloud plan includes live streaming. It is not encrypted end to end: everyone watching sees it, and so do our media servers (what WordPlus Cloud sees).

Whole examples: live streaming, interviews with guests on stage and members-only streams. How a stream starts, runs and ends: how calls and streams work.

Ann live in the shop show from its studio, beside Ben watching it: the picture, who is watching, and their chat on both sides. The cameras are illustrations

Who is who​

A live stream for each class, named after it: class-live:7.

Your plugin decides who is who in it. A page that opens the stream asks your site first, and a rule answers: a function your plugin declares once for every stream whose name fits class-live:{id}. WordPlus Cloud then holds the page to the answer. This rule says:

  1. The teacher hosts it: goes live, brings students on stage, keeps the chat and ends it.
  2. A student watches, writes in the chat and raises a hand.
  3. Anyone else is refused.
// The live streams of the classes, such as class-live:7.
wordplus_realtime()->streams( 'class-live:{id}', function ( $user_id, array $p, $name ) {
$class = (int) $p['id'];

// 1. The teacher, who hosts.
if ( my_school_teacher_of( $class ) === $user_id ) {
return 'host';
}

// 2. A student, who watches.
if ( my_school_student_of( $class, $user_id ) ) {
return 'viewer';
}

// 3. Anyone else.
return false;
}, array( 'reactions' => true ) );
  • The answer: false, 'host' or 'viewer', or an array for more:

    Key
    rolehost (goes live, brings people on stage, keeps the chat, ends it) or viewer
    infoname and an https:// avatar, as everyone sees them. The display name and picture of the user by default
    chatwrite or read
    modtrue for someone who keeps the chat without hosting
    srcwhat the host may send: camera, microphone, screen
    qualitythe best picture the host sends: 1080p, the default, or 720p
  • A visitor who is not signed in comes with $user_id 0, and may only be a viewer. A rule that gives them more is refused with no_user.

  • Names use letters, digits and - = @ , . ; :, up to 128 characters, as room names do but without _.

  • The settings of the stream, the third argument, each a value or a callable of the parts of the name:

    • stage: whether viewers come on stage as guests (below), yes unless false.
    • reactions: whether it takes reactions, yes unless false.
    • people: who may see who is watching, mods (the default) or all.
    • chat: array( 'offline' => …, 'links' => …, 'fresh' => … ). offline is 'open' or 'closed', for the chat while the stream is not live. links false takes links only from those who keep the chat. fresh true empties the chat each time the stream goes live.

The calls route the package registers answers streams too: nothing more to add.

Your site hears each stream go live and end by webhook, stream.live and stream.ended. stream.ended brings peak, the most watching at once, and viewer_seconds, the time of everyone watching it live, added up: the audience of the stream in all, as the site hears no event for each viewer (webhooks).

Drawn ready​

Optional, as every ready-made part: the rest of this page draws the stream with no element. This is the ready-made one.

The stream as Easy Stream draws it: the player, the chat beside it, and for its host the studio, with the camera and microphone, the picture before going live, OBS, Go live and End stream. Enqueue it on the pages that show a stream, and put the element where it goes:

add_action( 'wp_enqueue_scripts', function () {
if ( is_singular( 'class' ) ) {
wordplus_realtime()->stream_screen();
}
} );

// In the template of the class:
echo wordplus_realtime()->stream_element( 'class-live:' . get_the_ID(), array( 'title' => get_the_title() ) );
  • stream_element( $name, $attributes ) writes <live-stream>, escaped. Its parts apart, each where the page wants it: 'view' => 'player', 'chat' (with 'height' => 480), 'details' or 'studio'. The parts of one stream share one join.
  • 'view' => 'studio' writes <stream-studio>: the studio alone, with the camera and microphone, the picture before going live, streaming from OBS, Go live and End stream. It shows nothing to anyone your rule does not make the host.
  • Its attributes: title, cover (a picture behind the player before the stream starts and after it ends), url (the page to share), login-url (where a visitor signs in to chat, such as wp_login_url( get_permalink() )), studio-url (a page of its own where the host goes live, which the host then sees the way to in place of the studio), chat ('off') and sticky (true, the player kept at the top of a phone while live).
  • What the page user sees follows the answer of your rule: the studio for its host, the player and the chat for everyone else, with what keeps the chat for its mods.
  • A refusal shows its words in place of the stream, such as no access, or live streaming not in the plan.
  • Its words follow the language of the site, as the call screen does, with the same wordplus_realtime_screen_strings filter for words of your own.
  • Ringing: stream_screen() leaves calls as they are. A page where calls should not ring calls wordplus_realtime()->ui() too.
  • Its look is its own, drawn in the page: every class starts with estream-.

Watching​

The same stream on markup of your own. The page needs the calls script (wordplus_realtime()->calls_script()), as for calls:

const { calls } = wordplusRealtimeCalls.page();

const show = await calls.stream( 'class-live:7' ); // rejects with `forbidden` when your rule says no

show.watch( document.querySelector( '#live video' ) ); // the main picture of the stream, as it changes, and its sound
show.on( 'state', ( state ) => showState( state ) ); // idle, live, ended
show.on( 'viewers', ( n ) => showCount( n ) ); // how many watch, the host not counted
  • The script of the stream loads beside the rooms script the first time a page opens a stream: a page with calls alone never fetches it.
  • The main picture is the one the host chose: a shared screen, the picture from OBS, else the camera of the host. show.tiles has each picture with its own camera, screen and audio, for a layout of your own.
  • Sound plays from the page, and the video stays muted. Where the browser holds sound back until a click (show.snapshot().audioBlocked), call show.startAudio() from one.
  • video: false in calls.stream( name, { video: false } ) joins without the picture, for a page that shows the chat or the count. watch() fetches it later.
  • The quality: show.qualities() lists the layers a viewer may choose, the best first, each high, medium or low with its width and height, none above 720p. show.setQuality( 'medium' ) watches that layer whatever the size of the video, and 'auto', the default, picks by its size and the connection, up to Full HD for a big player.
  • How it plays: show.stats() is what this browser measures once a second while the picture comes: the picture and the sound that arrive, the media server and the round trip, how long the picture took to start, stalls and reconnects, and the last things that happened. It is sent nowhere, for a page that shows it or a person debugging.
  • A screen of your own: show.snapshot() is everything the screen of the stream draws, and show.subscribe( fn ) hears every change.
  • show.leave() leaves the stream, its picture and its chat.

Going live​

The page of the host shows the camera before going live, then goes live with whatever it has on:

if ( show.role === 'host' ) {
await show.preview(); // the camera and microphone, shown in watch( video )
await show.setCamera( false ); // or setMicrophone(), setScreenShare( true )
await show.goLive(); // everyone watching sees it now
// …
await show.end();
}

A host may take an ended stream live again under the same name.

From OBS or another encoder, over WHIP:

const { url, key } = await show.ingest();   // ingest( true ) makes a new key and cuts off the old one

The encoder streams to url with key, and its picture takes the place of the camera. The ready-made studio shows both, with the steps in OBS.

Guests on stage​

A viewer raises a hand, the host invites them, and they come on stage beside the host with their camera and microphone, for everyone watching: a question from the class, an interview, a panel. The host may invite anyone watching too. The ready-made player and studio show all of it: the raise hand button, the hands for the host, the invitation and the guest on stage. On markup of your own:

// The page of a viewer:
raiseButton.onclick = () => show.hand( true ); // show.hand( false ) takes it down
show.on( 'invited', () => offerToJoin() ); // the host asks them on
joinButton.onclick = () => show.joinStage(); // on stage, the camera and microphone live
leaveButton.onclick = () => show.leaveStage();
show.on( 'stage', ( on ) => showOnStage( on ) ); // whether this page is on stage now

// The page of the host:
show.on( 'hands', ( hands ) => drawHands( hands ) ); // [ { user, name, at } ], the oldest first
await show.invite( hands[ 0 ].user ); // or anyone watching, by their user id
await show.unstage( 34 ); // off stage, back to watching
await show.layout( 'speaker' ); // 'grid', 'speaker', or 'spotlight' with a user id
  • Everyone watching sees the guest in the picture of the stream, by the layout the host chose: everyone in a grid, the one speaking large, or one person in the spotlight.
  • A guest sends the camera and microphone, the picture up to 720p.
  • A visitor who is not signed in watches, and does not come on stage.
  • The stage is on unless the settings of the stream say 'stage' => false, which takes no hands and no invitations.

The chat and reactions​

show.on( 'message', ( m ) => addLine( m.from.name, m.text ) );   // each new message, show.messages has the last ones
await show.chat.send( 'Hello!' );
show.react( '❤️' ); // any one emoji

The ready-made player offers ❤️ 👏 😂 😮 🔥. Whoever keeps the chat, the host and anyone your rule made mod, keeps it from the page:

await show.chat.delete( id );
await show.chat.pin( id ); // show.chat.unpin() takes it down
await show.chat.timeout( 7, 600 ); // no writing for 10 minutes: from a minute to a day, they go on watching
await show.chat.slow( 30 ); // one message per 30 seconds, 0 turns it off
await show.chat.clear();
await show.kick( 7 ); // out now, free to come back
await show.ban( 7 ); // out, and kept out

A message the chat does not take, or an act the page may not do, rejects with its code (refusals).

From the server​

PHP reads a stream and keeps its chat as its moderators do, with no page open. Each change reaches everyone watching at once:

$show = wordplus_realtime()->stream( 'class-live:7' );

wordplus_realtime()->list_streams(); // those live now: array( array( 'name' => …, 'started_at' => …, 'viewers' => … ) )
$show->get(); // its state, viewers, peak, slow mode, reactions and pinned message
$show->people(); // array( 'watching' => 120, 'people' => array( array( 'user' => '7', 'name' => …, 'role' => 'viewer' ), … ) )
$show->chat( 50 ); // the last messages, oldest first
$show->say( 'The quiz starts in five minutes', 'Course bot', 'https://example.com/bot.png' );
$show->delete_message( $id );
$show->pin( $id ); // null unpins
$show->slow( 30 ); // 0 turns it off
$show->timeout( 7, 600 ); // untimeout( 7 ) lifts it
$show->kick( 7 ); // out now, free to come back
$show->ban( 7 ); // unban( 7 ) lifts it, bans() lists them
$show->reactions( false );
$show->end();

A message from the site shows as a host message, with the name and https picture you give it, so nobody may ban it. A host is never put out or timed out: the call answers a WP_Error (host).

Viewers and credits​

The first 50 watching the streams of a site at once take no credits. Each viewer above them takes a credit a minute from the account, only while the stream is live, and the host and the guests none. wordplus_realtime()->usage() says what the account has left (what the site uses).

Once the account has used its credits for the month, what it may borrow included, those watching go on watching, and new viewers above the 50 come in again as soon as it buys credits (charging for calls and streams).

Refusals​

calls.stream(), the acts of the host and of those who keep the chat, and a message sent reject with a code:

Code
forbiddenyour rule said no, or no pattern of a plugin matches the name
bad_requestnot a name a stream takes
no_useryour rule gave a visitor who is not signed in more than the place of a viewer
not_includedthe plan of the site does not include live streaming
bannedthis user is banned from the stream
no_hoursthe account has used its credits for the month, what it may borrow included: a new viewer above the 50 comes in once it buys credits (viewers and credits)
not_hostan act of the host, from someone your rule did not make the host
not_modan act of those who keep the chat, from someone else
hosta host is never put out or timed out
anonymousa hand raised by a visitor who is not signed in
no_stagea hand or an invitation in a stream whose settings say 'stage' => false
not_invitedjoinStage() with no invitation
devicesjoinStage() on a page that got no camera or microphone: it goes on watching
read_onlya message from someone your rule gave 'chat' => 'read'
slowa message sooner than slow mode allows, or within a second of the one before
timeouta message from someone timed out
too_longa message over 500 characters
linksa link, in a chat that takes links only from those who keep it
not_live, endeda message while the chat is closed, before the stream goes live or after it ended

What WordPlus Cloud sees​

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.