Video Call App built with :heart:Agora SDK:heart: and React
Video Call App built with :heart:Agora SDK:heart: and React
This repository has been deprecated, please use the demo provided by latest agora react rtc sdk
This tutorial describes how to add video chat to your ReactJS applications using the AgoraRTC SDK.
With this sample app, you can:
This section shows you how to prepare, build, and run the sample application.
To build and run the sample application, you must obtain an app ID:
src/agora.config.js file. In the AGORA_APP_ID declaration, update Your App ID with your app ID.export const AGORA_APP_ID = 'Your App ID'Download the Agora Web Video SDK. Unzip the downloaded SDK package and copy the AgoraRTC-*.js file into the sample application's /src/library/ folder. Rename the file to AgoraRTC.js.
Note: CDN can now be used to retrieve the latest SDK. You do not have to re-download SDK updates.
Open the terminal and navigate to your project folder.
cd path/to/projectnpm to install the dependencies:# install dependency
npm installUse start for a local build. View the application in your browser with the URL http://localhost:3000
# serve with hot reload at localhost:3000
npm run startUse build for a production version with minification.
# build for production with minification
npm run buildThe sample application uses assets located in the src/assets/images folder.
| Asset | Image | Description |
|---|---|---|
ag-index-background.png |
Main application background image. | |
ag-logo.png |
Logo image for the application. | |
avatar.png |
Avatar image for the application. | |
ag-login.png |
||
| Image of a monitor, for login. | ||
ag-audience.png and ag-audience-active.png |
|Image of a person icon in a circle, to represent an audience member.
ag-mic-s.png and ag-mic-active-s.png|
|Image of a microphone, to turn on/off audio.
ag-video-s.png and ag-video-active-s.png|
|Video camera image, to turn on/off video.
ag-oval.png and ag-oval-active.png|
|Circle image (empty and filled-in) to indicate selections.
The Index page is managed by the src/pages/index/index.jsx file.
The Index page is comprised of four classes:
| Class | Description |
|---|---|
Index |
Main class for the index page. The other classes are used in the layout for this class. |
InputChannel |
Text input manager for the channel name. |
BaseOptions |
Chooser for the type of call. |
AdvancedOptions |
Chooser for video transcoding. |
import React from 'react'
import * as Cookies from 'js-cookie'
import '../../assets/fonts/css/icons.css'
import Validator from '../../utils/Validator'
import { RESOLUTION_ARR } from '../../utils/Settings'
import './index.css'
class Index extends React.Component {
...
}
class InputChannel extends React.Component {
...
}
class BaseOptions extends React.Component {
...
}
class AdvancedOptions extends React.Component {
...
}
...
export default IndexThe Index class is main class for the index page.
class Index extends React.Component {
...
}The constructor() method initializes the state properties for the class:
| Property | Value | Description |
|---|---|---|
joinBtn |
false |
Indicates if the Join button is enabled / disabled. |
channel |
Empty string | Name of the channel. |
baseMode |
avc |
Indicates the current base mode for the call. |
transcode |
interop |
Indicates the current transcoding setting. |
attendeeMode |
video |
Indicates the current connection mode for the attendee. |
videoProfile |
480p_4 |
Indicates the current video profile setting. |
constructor(props) {
super(props)
this.state = {
joinBtn: false,
channel: '',
baseMode: 'avc',
transcode: 'interop',
attendeeMode: 'video',
videoProfile: '480p_4',
}
}The componentDidMount() method initializes event listener for keyboard presses.
When a keyboard button is pressed, ensure e.keyCode is the Enter / Return key before executing this.handleJoin().
componentDidMount() {
window.addEventListener('keypress', (e) => {
e.keyCode === 13 && this.handleJoin()
})
}The handleChannel() method updates the channel and joinBtn state properties.
/**
*
* @param {String} val 0-9 a-z A-Z _ only
* @param {Boolean} state
*/
handleChannel = (val, state) => {
this.setState({
channel: val,
joinBtn: state
})
}The handleJoin() method initiates joining the channel.
joinBtn state is false, otherwise stop the method execution with a return.state information using console.log.channel, baseMode, transcode, attendeeMode, and videoProfile.meeting page. handleJoin = () => {
if (!this.state.joinBtn) {
return
}
console.log(this.state)
Cookies.set('channel', this.state.channel)
Cookies.set('baseMode', this.state.baseMode)
Cookies.set('transcode', this.state.transcode)
Cookies.set('attendeeMode', this.state.attendeeMode)
Cookies.set('videoProfile', this.state.videoProfile)
window.location.hash = "meeting"
}The render() method displays the UI for the index page in the return().
The layout is comprised of three sections, specified by the class names ag-header, ag-main, and ag-footer.
The ag-main section contains the key login elements for the layout. The remaining code for this section is contained within the section sub-element.
The ag-footer section contains a Powered By Agora text and Agora contact information.
render() {
return (
)
}The login-header section contains the Agora logo, application title, and the application motto / subtitle.
The login-body section is divided into three main sections:
The first section is a text input box for the room name / channel. The InputChannel element is nested within two div elements. The text input has onChange event listener which invokes the this.handleChannel method.
The second section is contains a BaseOptions component and a AdvancedOptions component.
The BaseOptions component has an onChange event listener which updates the baseMode state value.
The AdvancedOptions component has two event listeners:
onRadioChange event listener which updates the transcode state value.OnSelectChange event listener which updates the videoProfile state value.
The third section is contains a set of radio dials and supporting images and text labels. Each radio input element has an onChange event listener which updates the attendeeMode state value.
The footer for the ag-main section contains a Join button, designated by an a element. The Join button has an onClick event listener which invokes the this.handleJoin method. This button is set to enabled / disabled by the joinBtn state value.
The InputChannel class defines the room channel text input for the index page.
class InputChannel extends React.Component {
...
}The constructor() method initializes the state properties errorMsg and state to empty strings.
constructor(props) {
super(props)
this.state = {
errorMsg: '',
state: ''
}
}The validate() method checks the string for the text input and updates the state and errorMsg properties if necessary.
Set both properties to an empty string.
validate = (val) => {
this.setState({
state: '',
errorMsg: ''
})
...
}Ensure the text input value is not empty using Validator.isNonEmpty. If the value is empty, update the errorMsg and state properties to inform the user that the text input should not be empty.
if (Validator.isNonEmpty(val.trim())) {
this.setState({
errorMsg: 'Cannot be empty!',
state: 'is-danger'
})
return false
}Ensure the text input value at least 1 character long. If the value is shorter than 1 character, update the errorMsg and state properties to inform the user that the text input should be longer than 1 character.
else if (Validator.minLength(val.trim(), 1)) {
this.setState({
errorMsg: 'No shorter than 1!',
state: 'is-danger'
})
return false
}Ensure the text input value 16 characters or shorter. If the value is longer than 16 characters, update the errorMsg and state properties to inform the user that the text input should no longer than 16 characters.
else if (Validator.maxLength(val.tNo open issues yet, or sync has not completed.