Hire the author: Sunny G
Introduction
In this blog, I will talk about how to make a real-time communication app using the agora package. It can be implemented using the npm Agora RTC package which is used to send and receive audio/video of the user. I will walk you through all the things step by step. For sharing audio/video we used the agora-rtc-sdk-ng package.
You can easily set up this package in your project. For video sharing, agora is best to fulfil your requirements. It provides full customization. As we know virtual event is a trendy market right now. Agora SDK is best to create a virtual event platform. Agora is also good at live engagement and interaction. They specialize in live video, audio, and broadcasting. Built using SD-RTN. They own the network and can provide the flexibility and support that no other provider gives.
What is the agora rtc package?
Agora NG Web-SDK is a full-featured SDK to implement streaming. It is an updated version of agora-rtc-sdk. ng package is completely different from the agora-rtc-sdk. If you migrate from RTC SDK to SDK ng, you need to put lots of effort to achieve this. The previous version of the Agora RTC SDK library is not preferable to use.
The main update in the new version is that they replaced the Stream object with the Local Track and Remote Track objects for controlling the media stream. Other features you can implement with this app are Image beautification, live streaming, screen sharing, and sharing audio/video tracks.
Learning Strategies and Tools
To create a basic application these are some prerequisites that you must need to keep in mind.
- Angular 2+ knowledge
- Typescript
- Node.js commands
- Agora RTC sdk ng package
- agora RTC token
- Create new project here console.agora.io. After that you will get appID which you can use to run the app.
- visual studio code
Project Setup
Install Node and run the below commands after that
To install angular
>npm install -g @angular/cli
To create a new application
>ng new agora-app
To change directory
>cd agora-app
To run the application
>ng serve
To install agora rtc dependencies
>npm i agora-rtc-sdk-ng
File structure
To create a video call app The core things that we require are services and components.
API service includes:
As I mentioned above we need an agora RTC token to start a call. Here we are going to use the API with this API you can generate RTC tokens. So, This service is used to run the HTTP request to get the RTC token.
Stream service includes:
This service is used to add RTC utilities and store variables, create RTC clients etc. It is also used to import packages so that you can do the call initialization or other kinds of stuff. Here we are going to handle how we can play the video and audio of users.
The basic component includes:
The component is to display the UI of joined users. Created two buttons Join and Leave. containers to show video of users.
Routing file includes:
Create routes so that multiple users can join the call. As we know to join a call we need a link so that users can join the call. So the route is a link that we need to create also.
Step by Step Procedure
Step 1: using agora RTC SDK utilities/services-
To start a call, you need to create an RTC client that provides functions to connect your system to Agora servers. With the Agora create client method it creates a local client object with your appID. After this creation you need to call the Agora join method then you have to create an instance of local tracks or a media stream. A media stream consists of an audio and video track. This package controls the stream by operating the tracks. It is built using webRTC.
With this, you need to create a local audio-video stream to send to the remote user. If we want to display the stream on our screen then pass the DOM element id where you want to show video in HTML template in the play method.
Here we have to maintain a remote user ids list so that we can track easily who has joined the channel. AgoraRTCClient object is the main utility or service that provides various functions to join a call, publish local audio and video tracks etc.
Local Track and Remote Track objects provide methods for controlling audio, video, basically playback control.
Step 2:creating client roles
In this library, two types of client roles are present- host, audience.
All users are host by default. When the user publishes a stream then events start work. It is important to declare a role if you are creating a hosting app. call the setClientRole method to set the user role as host before calling publish method.
If the user role is the audience then the user can only watch and not able to publish their tracks.
The package does not automatically set the user role as host before calling publish method. you need to set the client role host before calling this method.
Note: If the stream is published, they automatically become hosts, if it is unpublished, they automatically become an audience.
You can refer more to Agora RTC’s basic processes and objects in the below code. I will also give you all code in the end for your reference.
Step 3: Adding Functions and agora events
To utilize Agora functions, you have to use the async-await method to handle the Join and Leave button event.
When a remote user publishes media tracks, the SDK triggers user-published event, in which you can get an Agora RTC Remote User object. You need to listen for this event with client.on and call Agora RTC Client.subscribe in the callback to subscribe to this remote user.
To automatically subscribe to the remote user who publishes media tracks, and play the media tracks, we add the following code to listen for user-published.
When the remote user unpublished a media track or leaves the channel, the SDK triggers user-unpublished event.
Step 4: Creating rtc streaming angular service
rtcstream.service.ts
Step 5: Creating component to start a call-
app.component.html
Step 6: Integrating service to the component to start a call
app.component.ts
Now we have successfully created a simple video calling. you can feel free to change the styles and add more features like Image enhancement, etc.
Future Directions
With this package, you can create below functionalities:
- video call Applications (conferencing tool, Group video calling)
- voice call Applications.
- live interactive voice/video streaming app – You can broadcast a live video in your app. In this single person stream, the video and other people can see it.
- live podcasting app
Reflective Analysis
The end result is below. Here the feature we are have added is On the start call button click the User can join the call with his audio/video. Simple to use app:
How to Test this Video App
Run the app using ng serve command.
After this, you can open these links in two tabs.
Click on the join now button to connect the call
Glossary
Here’s an video app glossary:
- RTC: real time communication on web
- SD-RTN: Software Defined Real-time Network, is a real-time transmission network built by Agora.
- appID: project id that you create on Agora console
- stream: kind of data stream of video and audio tracks
Conclusion
It is simple to create a basic video app with these Agora packages. You can integrate this with any client-side framework. Here i have integrated the packages using the angular framework.
More Resources
GitHub repository for the source code – check the master branch in this repo.
For more info you can check:
reference link:
I also invite you to join the Agora.io Developer Slack community:
For more interesting blogs check: LD blog
This is a short & precise article; I like how it’s easy to add more users to an ongoing video call when hosted locally.