Thesass-loaderwill resolve the@importstatements and include the imported Sass in the resulting CSS, hence the resulting CSS will probably not have any import statements. But thecss-loaderdoes not just handle imports. The three loaders you mention, do very different things and they are meant to be used together, although there are other loaders you could use, which would give a similar result.
sass-loader - Transforms Sass to CSS. (Input: Sass, Output: CSS)
css-loader - Transforms CSS to a JavaScript module. (Input: CSS, Output: JavaScript)
style-loader - Injects the CSS, that is exported by the JavaScript module, into a <style> tag at runtime. (Input: JavaScript, Output: JavaScript).
Note: The output of style-loader is only relevant if you are using CSS modules, which passes on the object that maps class names to local identifiers.
My first attempt at WebRTC was to implement a video call feature within a Laravel Application. The implementation involved placing a call, showing an incoming call notification, and the ability of the receiver to accept the call. I wrote about it over here:
One of my readers asked whether it was possible to build a live streaming application with WebRTC in a Laravel Application. I took up this challenge and even though WebRTC has limitations, I came up with a simple live streaming implementation.
We'll go through my implementation in this article.
This tutorial assumes you know how to set up a new Laravel project with VueJs authentication. Create some users after setting up your project. You should be familiar with Laravel's broadcasting mechanism and have a fair idea of how WebSockets work. You may use this starter project I created: Laravel 8 Vue Auth Starter
Add routes for streaming pages in routes/web.php. The routes will be used to visit the live stream page, start a live stream from the device camera, and generate a broadcast link for other authenticated users to view your live stream.
Create Dynamic Presence and Private Channel in routes/channels.php.
Authenticated users subscribe to both channels. The presence channel is dynamically created with a streamId generated by the broadcaster. This way, we can detect all users who have joined the live broadcast.
Signaling information is exchanged between the broadcaster and the viewer through the private channel.
// Dynamic Presence Channel for StreamingBroadcast::channel('streaming-channel.{streamId}',function($user){return['id'=>$user->id,'name'=>$user->name];});// Signaling Offer and Answer ChannelsBroadcast::channel('stream-signal-channel.{userId}',function($user,$userId){return(int)$user->id===(int)$userId;});
Create StreamOffer and StreamAnswer events. Signaling information is broadcast on the stream-signal-channel-{userId} private channel we created early on.
The broadcaster sends an offer to a new user who joins the live stream when we emit the StreamOffer event and the viewer replies with an answer using the StreamAnswer event.
Add the following code to app/Events/StreamOffer.php.
<?phpnamespaceApp\Events;useIlluminate\Broadcasting\InteractsWithSockets;useIlluminate\Broadcasting\PrivateChannel;useIlluminate\Contracts\Broadcasting\ShouldBroadcast;useIlluminate\Foundation\Events\Dispatchable;useIlluminate\Queue\SerializesModels;classStreamOfferimplementsShouldBroadcast{useDispatchable,InteractsWithSockets,SerializesModels;public$data;/**
* Create a new event instance.
*
* @return void
*/publicfunction__construct($data){$this->data=$data;}/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/publicfunctionbroadcastOn(){// stream offer can broadcast on a private channelreturnnewPrivateChannel('stream-signal-channel.'.$this->data['receiver']['id']);}}
Add the following code to app/Events/StreamAnswer.php.
<?phpnamespaceApp\Events;useIlluminate\Broadcasting\InteractsWithSockets;useIlluminate\Broadcasting\PrivateChannel;useIlluminate\Contracts\Broadcasting\ShouldBroadcast;useIlluminate\Foundation\Events\Dispatchable;useIlluminate\Queue\SerializesModels;classStreamAnswerimplementsShouldBroadcast{useDispatchable,InteractsWithSockets,SerializesModels;public$data;/**
* Create a new event instance.
*
* @return void
*/publicfunction__construct($data){$this->data=$data;}/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/publicfunctionbroadcastOn(){returnnewPrivateChannel('stream-signal-channel.'.$this->data['broadcaster']);}}
Create WebrtcStreamingController to handle the broadcasting, viewing, and signaling for the live stream.
Let's explore what the methods in the controller are doing.
index: This returns the view for the broadcaster. We pass a 'type': broadcaster and the user's ID into the view to help identify who the user is.
consumer: It returns the view for a new user who wants to join the live stream. We pass a 'type': consumer, the 'streamId' we extract from the broadcasting link, and the user's ID.
makeStreamOffer: It broadcasts an offer signal sent by the broadcaster to a specific user who just joined. The following data is sent:
broadcaster: The user ID of the one who initiated the live stream i.e the broadcaster
receiver: The ID of the user to whom the signaling offer is being sent.
offer: This is the WebRTC offer from the broadcaster.
makeStreamAnswer: It sends an answer signal to the broadcaster to fully establish the peer connection.
broadcaster: The user ID of the one who initiated the live stream i.e the broadcaster.
answer: This is the WebRTC answer from the viewer after, sent after receiving an offer from the broadcaster.
Configuring Frontend
Instantiate Laravel Echo and Pusher in resources/js/bootstrap.js by uncommenting the following block of code.
Create resources/js/helpers.js. Add a getPermissions function to help with permission access for the microphone and camera. This method handles the video and audio permission that is required by browsers to make the video calls. It waits for the user to accept the permissions before we can proceed with the video call. We allow both audio and video. Read more on MDN Website.
exportconstgetPermissions=()=>{// Older browsers might not implement mediaDevices at all, so we set an empty object firstif(navigator.mediaDevices===undefined){navigator.mediaDevices={};}// Some browsers partially implement media devices. We can't just assign an object// with getUserMedia as it would overwrite existing properties.// Here, we will just add the getUserMedia property if it's missing.if(navigator.mediaDevices.getUserMedia===undefined){navigator.mediaDevices.getUserMedia=function(constraints){// First get ahold of the legacy getUserMedia, if presentconstgetUserMedia=navigator.webkitGetUserMedia||navigator.mozGetUserMedia;// Some browsers just don't implement it - return a rejected promise with an error// to keep a consistent interfaceif(!getUserMedia){returnPromise.reject(newError("getUserMedia is not implemented in this browser"));}// Otherwise, wrap the call to the old navigator.getUserMedia with a PromisereturnnewPromise((resolve,reject)=>{getUserMedia.call(navigator,constraints,resolve,reject);});};}navigator.mediaDevices.getUserMedia=navigator.mediaDevices.getUserMedia||navigator.webkitGetUserMedia||navigator.mozGetUserMedia;returnnewPromise((resolve,reject)=>{navigator.mediaDevices.getUserMedia({video:true,audio:true}).then(stream=>{resolve(stream);}).catch(err=>{reject(err);// throw new Error(`Unable to fetch stream ${err}`);});});};
Create a component for the Broadcaster named Broadcaster.vue in resources/js/components/Broadcaster.vue.
The logic for this live streaming application can be likened to a group video call where only one person's stream is seen.
The stream of the broadcaster is rendered on the viewers' browser but the broadcaster doesn't receive anything from the viewers after exchanging the signaling information which is required in WebRTC.
This looks like a star topology and there is a limitation on how many peers can be connected to a single user.
I want to explore the option of turning some of the viewers into broadcasters after the initial broadcaster's peer has connected to about 4 users.
The goal is to rebroadcast the stream they received from the original broadcaster.
Is it possible? I can't tell. This will be an interesting challenge to explore.