This document describes how to implement an audio and video call using React.
Basic concepts:
Before you begin, make sure you complete the following steps:
The current project uses Node 14.17.3 and React 16.7.0.
The following diagram shows the basic process of User A playing a stream published by User B:
The following diagram shows the API call sequence of the stream publishing and playing process:
1. Optional: Create the UI
Before creating a ZegoExpressEngine
instance, we recommend you add the following UI elements to implement basic real-time audio and video features:
2. Create a ZegoExpressEngine
instance
To create a singleton instance of the ZegoExpressEngine
class, pass in your AppID as the appID
parameter and the Server URL as the server
parameter. You can obtain them from the ZEGOCLOUD Admin Console.
Import the react.js
, react-dom.js
, and babel.js
to the /express-demo-web/src/Examples/Framework/React/index.html
file.
// Import the react.js, react-dom.js.
<script src="https://cdn.bootcdn.net/ajax/libs/react/16.7.0/cjs/react.development.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/react-dom/16.7.0/cjs/react-dom-server.browser.development.js"></script>
// Import the babel.min.js using ES6 scripting language.
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
If you need to use JSX, set the type
property of the <script> tag to text/babel
.
Initialize the ZegoExpressEngine
instance.
// Initialize the [ZegoExpressEngine] instance.
class CommonUsageReact extends React.Component {
constructor(props) {
super(props);
this.state = {
zg: null
}
}
createZegoExpressEngineOption(){
const zg = new ZegoExpressEngine(appID, server)
this.setState({
zg:zg
},() => {
// Listen for the event callbacks.
this.initEvent();
})
}
}
3. Optional: Listen for and handle the event callbacks
After you create a ZegoExpressEngine
instance, you can call the on
method to listen for and handle various event callbacks as needed.
The following sample code demonstrates how to listen for and handle the roomStateUpdate
callback. For more event callbacks, see ZegoRTCEvent
and ZegoRTMEvent
.
initEvent() {
this.state.zg.on('roomStateUpdate', (roomID, state, errorCode, extendedData) => {
if (state == 'DISCONNECTED') {
// Disconnected from the room.
// ...
}
if (state == 'CONNECTING') {
// Connecting to the room.
// ...
}
if (state == 'CONNECTED') {
// Connected to the room.
// ...
}
})
}
Before starting to publish or play a stream, you can call the checkSystemRequirements
method to check if your browser supports WebRTC.
const result = await this.zg.checkSystemRequirements();
// The [result] indicates whether it is compatible. It indicates WebRTC is supported when the [webRTC] is [true]. For more results, see the API documents.
console.log(result);
// {
// webRTC: true,
// customCapture: true,
// camera: true,
// microphone: true,
// videoCodec: { H264: true, H265: false, VP8: true, VP9: true },
// screenSharing: true,
// errInfo: {}
// }
For more about the parameters of the returned results, refer to the parameter description of the ZegoCapabilityDetection
method.
1. Obtain the login token
When logging in to a room, you need to pass in a login token for user authentication. For information about how to obtain the login token, see User privilege control.
2. Log in
To log in to a room, call the loginRoom
with the following parameters:
roomID
parametertoken
parameterroomID
and userName
parameterconfig
parameter based on the actual situation.roomID
, userID
, and userName
.roomID
and userID
must be globally unique within the scope of the AppID. We recommend you set the userID
to a meaningful value. You can associate userID
with the account system of your application. // Log in to a room. It returns `true` if the login is successful.
// The roomUserUpdate callback is disabled by default. To receive this callback, you must set the `userUpdate` property to `true` when logging in to a room.
const result = await this.state.zg.loginRoom(roomID, token, {userID, userName}, {userUpdate: true});
3. Listen for and handle the event callbacks related to room users and streams
To listen for and handle various events that may happen after logging in to a room, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to room users and streams:
roomStateUpdate
: Callback for updates on current user's room connection status. When the current user's room connection status changes (for example, when the current user is disconnected from the room or login authentication fails), the SDK sends out the event notification through this callback.
roomUserUpdate
: Callback for updates on the status of other users in the room. When other users join or leave the room, the SDK sends out the event notification through this callback.
roomStreamUpdate
: Callback for updates on the status of the streams in the room. When new streams are published to the room or existing streams in the room stop, the SDK sends out the event notification through this callback.
roomUserUpdate
callback, you must set the isUserStatusNotify
property of the room configuration parameter ZegoRoomConfig
to true
when you call the loginRoom
method to log in to a room. roomUserUpdate
callback, and when there is a stream added, call the startPlayingStream
method to start receiving and playing the newly added stream.// Callback for updates on the current user's room connection status.
this.state.zg.on('roomStateUpdate', (roomID,state,errorCode,extendedData) => {
if (state == 'DISCONNECTED') {
// Disconnected from the room.
}
if (state == 'CONNECTING') {
// Connecting to the room.
}
if (state == 'CONNECTED') {
// Connected to the room.
}
})
// Callback for updates on the status of ther users in the room.
this.state.zg.on('roomUserUpdate', (roomID, updateType, userList) => {
console.warn(
`roomUserUpdate: room ${roomID}, user ${updateType === 'ADD' ? 'added' : 'left'} `,
JSON.stringify(userList),
);
});
// Callback for updates on the status of the streams in the room.
this.state.zg.on('roomStreamUpdate', async (roomID, updateType, streamList, extendedData) => {
if (updateType == 'ADD') {
// New stream added, start playing the stream.
} else if (updateType == 'DELETE') {
// Stream deleted, stop playing the stream.
}
});
1. Create a stream
a. To create a local audio and video stream, call the createZegoStream method. By default, the engine captures video data from the camera and captures audio data from the microphone.
After calling the createZegoStream method, you need to wait for the ZEGO server to return the media stream object (localStream
) before any further operation.
Set the ref
property of the video
tag object.
Create a container <div>
on HTML for the media streaming player component.
<div id="local-video" style="width: 320px;height: 240px"></div>
Play preview of the Stream.
// After calling the createZegoStream method, you need to wait for the ZEGO server to return the local stream object before any further operation.
const localStream = await this.zg.createZegoStream();
localStream.playVideo(document.querySelector("#local-video"), {enableAutoplayDialog:true});
b. Optional: Set up the audio/video capturing parameters
If you don't want to use the SDK's default audio/video capturing settings, you configure the following parameters of the createZegoStream method with your own specific settings. For more details, see Custom video capture.
2. Start publishing a stream
To start publishing a local audio and video stream to remote users, call the startPublishingStream
method with the following parameters:
streamID
parameterlocalStream
parameterstreamID
and make sure the streamID
is globally unique within the scope of the AppID. startPublishingStream
method multiple times, making sure that each stream has a unique streamID
.// [localStream] is the MediaStream object created by calling creatStream in the previous step.
this.state.zg.startPublishingStream(streamID, localStream)
3. Listen for and handle the event callbacks related to stream publishing
To listen for and handle various events that may happen after stream publishing starts, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to stream publishing:
publisherStateUpdate
: Callback for updates on stream publishing status. After stream publishing starts, if the status changes, (for example, when the stream publishing is interrupted due to network issues and the SDK retries to start publishing the stream again), the SDK sends out the event notification through this callback.
publishQualityUpdate
: Callback for reporting stream publishing quality. After stream publishing starts, the SDK sends out the streaming quality data (resolution, frame rate, bit rate, etc.) regularly through this callback.
this.state.zg.on('publisherStateUpdate', result => {
// Callback for updates on stream publishing status.
// ...
})
this.state.zg.on('publishQualityUpdate', (streamID, stats) => {
// Callback for reporting stream publishing quality.
// ...
})
1. Start playing a stream
To start playing a remote audio and video stream from the ZEGO server, call the startPlayingStream
method with the corresponding stream ID passed to the streamID
parameter.
You can obtain the stream ID of the streams published by remote users from the callback roomStreamUpdate
.
Set the ref
property of the video
tag object.
<video ref="remoteVideo" autoplay playsinline :muted="true"></video>
const remoteStream = await this.state.zg.startPlayingStream(streamID);
Assign the remoteVideo
to the srCObject
property.
// The remoteVideo object is the <video> or <audio> element on your webpage.
this.$refs['remoteVideo'].srcObject = remoteStream;
streamID
must be globally unique within the AppID. remoteVideo
object is the audio or video element on your webpage, which will be used to play out the audio or video. We recommend you enable the autoplay
property of the remoteVideo
object.autoplay
property. In this case, we recommend you guide the users to manually unmute the video. 2. Listen for and handle the event callbacks related to stream playing
To listen for and handle various events that may happen after stream playing starts, you can implement the corresponding event callback methods of the event handler as needed. The following are some common event callbacks related to stream playing:
playerStateUpdate
: Callback for updates on stream playing status. After stream playing starts, if the status changes (for example, when the stream playing is interrupted due to network issues and the SDK retries to start playing the stream again), the SDK sends out the event notification through this callback.
playQualityUpdate
: Callback for reporting stream playing quality. After stream playing starts, the SDK sends out the streaming quality data (resolution, frame rate, bit rate, etc.) regularly through this callback.
this.state.zg.on('playerStateUpdate', result => {
// Callback for updates on stream playing status.
// ...
})
this.state.zg.on('playQualityUpdate', (streamID,stats) => {
// Callback for reporting stream playing quality.
})
We recommend you run your project on a real device. If your app runs successfully, you should hear the sound and see the video captured locally from your device.
To test out the real-time audio and video features, visit the ZEGO Express Web Demo, and enter the same AppID
, Server
and RoomID
to join the same room. If it runs successfully, you should be able to view the video from both the local side and the remote side, and hear the sound from both sides as well.
In audio-only scenarios, no video will be captured and displayed.
1. Stop publishing a stream
To stop publishing a local audio and video stream to remote users, call the stopPublishingStream
method with the corresponding stream ID passed to the streamID
parameter.
this.state.zg.stopPublishingStream(streamID)
2. Destroy a local media stream
To destroy a local media stream, call the destroyStream
method.
To stop the local video preview, you need to manually destroy the video element after destroying the local media stream.
// localStream is the MediaStream object created when calling the createZegoStream method.
this.state.zg.destroyStream(localStream)
3. Stop playing a stream
To stop playing a remote audio and video stream, call the stopPlayingStream
method with the corresponding stream ID passed to the streamID
parameter.
this.state.zg.stopPlayingStream(streamID)
To log out of a room, call the logoutRoom
method with the corresponding room ID passed to the roomID
parameter.
this.state.zg.logoutRoom(roomID)