Author: Kofi Ocran. I am a software engineer who builds products and services with web technologies. I occupy the sweet spot between software engineering and data science and learn more by sharing my knowledge through technical articles.
Laravel is a powerful language that aims to make the web development process simple without sacrificing application functionality. This is especially true when you try to build a Laravel video chat app.
Previously, I built a WebRTC/Laravel chat app and wrote about it here: Adding Video Chat To Your Laravel App. WebRTC is only one of the ways that people can implement video chat features. Companies like Agora also provide a fully packaged video chat SDK to provide a high-quality Real-Time Engagement video chat experience. As someone who has WebRTC development experience, I can tell you there are some limitations with WebRTC, such as:
After I was introduced to Agora, I was impressed that setting up the same video call feature is easier with Agora than with WebRTC. Let me walk you through Laravel chat app tutorial with Agora.
After creating Laravel chat app with Agora, I want to highlight some of the advantages:
composer require pusher/pusher-php-server "~4.0"
npm install --save laravel-echo pusher-js
We will set up the various controllers and classes with methods needed to generate the Agora token to establish a call. Laravel’s broadcasting system will also be activated.
We begin with the endpoints that will be accessed from the front end.
Add the following code to routes/web.php:
Uncomment BroadcastServiceProvider in config/app.phpin your project folder:
Change
//App\Providers\BroadcastServiceProvider::class
to
App\Providers\BroadcastServiceProvider::class
We get to know online users by looking at who has subscribed to this channel on the front end. We return their ID and name:
This event will be used to make a call by broadcasting some data over the agora-online-channel.
Run the following command in your terminal/console:
php artisan make:event MakeAgoraCall
Add the following code to MakeAgoraCall.php
cd app
mkdir -p Class/AgoraDynamicKey
Next, create the AgoraVideoController using the command line:
php artisan make:controller AgoraVideoController
Add the following code to AgoraVideoController.php:
index
: To view the video call page.token
: To generate the Agora dynamic token. The token generation code is taken from sample/RtcTokenBuilderSample.php, which can be found in the files downloaded during the project setup.callUser
: To call a particular user by broadcasting a MakeAgoraCall event across a Laravel presence channel that I’ve named agora-online-channel. All logged-in users subscribe and listen to events on this channel.The data received from the MakeAgoraEvent by the users subscribed to the agora-online-channel contains the following:
userToCall
: This is the private ID of the user who is supposed to receive a call from a caller.channelName
: This is the call channel that the caller has already joined on the front end. This is a channel created with the Agora SDK on the front end. It is the room the caller has already joined, waiting for the callee to also join to establish a call connection.from
: The ID of the caller.From the MakeAgoraEvent, a user can determine whether they are being called if the userToCall value matches their ID. We show an incoming call notification with a button to accept the call.They know who the caller is by the value of from.
We are going to create the Laravel chat app UI for making and receiving the video call with the ability to toggle the on and off states of the camera and the microphone.
Add the following < script src=”https://cdn.agora.io/sdk/release/AgoraRTCSDK-3.3.1.js">< /script> to the head tag of resources/views/layouts/app.blade.php.
It then becomes:
Instantiate Laravel Echo and Pusher in resources/js/bootstrap.js by uncommenting the following block of JavaScript code:
On your terminal or command line, create a component called AgoraChat.vue with the following command:
touch resources/js/components/AgoraChat.vue
Add the following code:
On the AgoraChat page, we display buttons that bear the name of each registered user and whether they are online or offline at the moment.
To place a call, we click the button of a user with online status. An online user indicates one who is readily available to receive a call. For our demo, we see a list of users. The user named Bar is indicated as being online. The caller named Foo can call Bar by clicking the button.
Bar gets an incoming call notification with Accept and Decline buttons and the name of the caller.
From the call notification image above, we see that the caller’s name is Foo. Bar can then accept the call for a connection to be established.
The following diagram explains the call logic in terms of the code:
Add the following code to resources/js/app.js:
On your terminal or command line, create a view called agora-chat.blade.php with the following command:
touch resources/views/agora-chat.blade.php
Add the following code to the agora-chat.blade.php:
The .env file is located at the root of your project folder. Add the credentials you got from Agora and Pusher:
php artisan serve
npm run dev
To confirm that your demo is functioning properly, see my demo video as an example of how the finished project should look and function: Agora Video Call Demo
You have now implemented the video chat feature in Laravel chat! It’s not that hard, right?
To include calling in a Laravel video streaming web app, you don’t have to build it from scratch.
Agora provides a lot of great features, all in one package. It also helps businesses save development hours when implementing video chat into existing applications. The only thing a developer has to do is build a compelling front end — Agora handles the video chat back end.
Link to project repository: https://github.com/Mupati/laravel-video-chat
Demo link: https://laravel-video-call.herokuapp.com/agora-chat
Make sure the demo link or production version is served on HTTPS.
Test accounts:
foo@example.com: DY6m7feJtbnx3ud
bar@example.com: Me3tm5reQpWcn3Q
I also invite you to join the Agora Developer Slack community.