Live streams in Laravel
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, a phone or OBS. Viewers may be signed in or not.
Who is who
Your app declares streams beside its rooms and calls, in boot() of a service provider. The rule says who each person is in the stream:
$this->callAfterResolving( Realtime::class, function ( Realtime $realtime ) {
// The show of the shop, show:mugs: Ann presents it, and everyone else watches and writes in its chat, signed in or not.
$realtime->streams( 'show:{id}', function ( $user, array $p ) {
if ( 'mugs' !== $p['id'] ) {
return false;
}
return null !== $user && 'Ann' === User::find( $user )?->name ? 'host' : 'viewer';
}, [ 'reactions' => true ] );
} );
- The answer:
false,'host'or'viewer', or an array:role,info(what others see of them),chat(writeorread),mod(keeps the chat),srcandquality(what the host sends). - A visitor who is not signed in comes with
$usernull, and may only be a viewer. - The settings of the stream, the third argument:
reactions,peopleandchat(offline,links,fresh), each a value or a closure of the parts of the name (live streams in depth).
The calls route of the browser (/wordplus/realtime/calls) answers streams too: nothing more to add.
The page
const calls = await rt.calls();
const show = await calls.stream( 'show:mugs' );
show.watch( document.querySelector( '#show video' ) ); // the picture and its sound
show.on( 'state', ( state ) => draw( state ) ); // idle, live, ended
show.on( 'viewers', ( n ) => ( count.textContent = `${ n } watching` ) );
show.on( 'message', ( m ) => addLine( m.from.name, m.text ) );
await show.chat.send( 'Hello!' );
if ( show.role === 'host' ) {
await show.preview(); // the camera, shown in the same video
await show.goLive();
}
The show card of the Example Shop does exactly this, in showCard() of resources/js/shop.js.
More
Reactions, keeping the chat, OBS, and the refusals: live streams in depth.