logo
Video Call
On this page

Multiplayer Video Call

2024-01-04

Feature Overview

This document demonstrates how to use ZEGO Express SDK to build a multiplayer video call scenario, which implements multi-to-many real-time audio and video interactions. Users can conduct real-time video calls with other users in the Room, and publish and play streams with each other. This scenario can be used for multiplayer real-time video chat, video conferences, etc.

Prerequisites

Before applying the multiplayer video call scenario, ensure that:

Example Source Code Download

Please refer to Download Example Source Code to get the source code.

For related source code, please check the files in the "src/Examples/Scenes/VideoForMultipleUsers" directory of ZEGO Express SDK.

Usage Steps

This section will introduce how to use ZEGO Express SDK to implement multiplayer video calls.

  • The process flow is as follows:
  • The API call sequence diagram is as follows:
Note

ZEGO Express SDK supports multiplayer video calls. The above sequence diagram takes real-time video calls between 2 room members as an example. It is recommended that developers refer to the above process to design their own multiplayer real-time video call scenarios.

1 Create Engine

Create a ZegoExpressEngine engine instance, pass the applied AppID to the parameter "appID", and pass the access server address to the parameter "server".

Note
  • "server" is the access server address. For how to obtain it, please refer to Console - Project Information.
  • For SDK version 3.6.0 and above, server can be changed to an empty string, null, undefined, or any character, but cannot be left empty.
const zg = new ZegoExpressEngine(appID, server);

2 Enable Room User Change Notification

When each user calls the loginRoom interface to log in to the room, developers need to set "userUpdate" in ZegoRoomConfig to "true" to receive callback notifications when other users enter or leave the room (i.e., roomUserUpdate).

const isLogin = await zg.loginRoom(
    roomID,
    token,
    { userID },
    { userUpdate: true }
  );

3 Listen to Callback Events

To implement the multiplayer video call function, you need to listen to the addition and deletion of users and streams in the room and handle them accordingly. Developers can implement certain methods in ZegoEvent (including ZegoRTCEvent and ZegoRTMEvent) according to actual needs. After creating the engine, you can set callbacks by calling the on interface.

Listen to User Changes in the Room

Warning

You can only listen to the roomUserUpdate callback when you set to focus on user changes when calling loginRoom to log in to the room, that is, when "userUpdate" in ZegoRoomConfig is set to "true" (the default value is "false").

To listen to user changes in the room, you need to register the roomUserUpdate callback. The addition and deletion of users who have logged in to the room will trigger this callback. Developers can implement their own business logic in the callback according to actual needs.

The "updateType" parameter in the callback indicates the type of user change in the room. The values of this parameter are as follows:

User Change TypeEnum ValueDescription
User AddedADDUsers are added to the room (i.e., joining the room), and "userList" is the list of added users.
User DeletedDELETEUsers are removed from the room (i.e., leaving the room), and "userList" is the list of removed users.

When a user logs in to the room for the first time, if there are other users in the room, the newly logged-in user will receive the list of added users through this callback, that is, the callback with "updateType" as "ADD". The user list is the users who already exist in the room.

zg.on('roomUserUpdate', (roomID, updateType, userList) => {
    console.log('roomUserUpdate roomID ', roomID, streamList);
    if (updateType === 'ADD') {
        // update view
    } else if(updateType == 'DELETE') {
        // update view
    }
});

Listen to Stream Changes in the Room

Warning

When a stream is deleted, if you are calling the startPlayingStream interface to play the stream, please call the stopPlayingStream interface to stop playing the stream. Otherwise, the SDK will report a stream playing error.

To listen to stream changes in the room, you need to register the roomStreamUpdate callback. The addition and deletion of streams in the room that have been logged in will trigger this callback. Developers can implement their own business logic in the callback according to actual needs.

The "updateType" parameter in the callback indicates the type of stream change in the room. The values of this parameter are as follows:

Stream Change TypeEnum ValueDescription
Stream AddedADDStreams are added to the room, and "streamList" is the list of added streams.
Stream DeletedDELETEStreams are removed from the room, and "streamList" is the list of removed streams.

When a user logs in to the room for the first time, if there are other users publishing streams in the room, a list of added streams will be received, that is, the callback with "updateType" as "ADD".

zg.on('roomStreamUpdate', (roomID, updateType, streamList) => {
    console.log('roomStreamUpdate roomID ', roomID, streamList);
    if(updateType === 'ADD') {
    	// update view
    } else if(updateType == 'DELETE') {
    	// update view
    }
});

4 Other Operations

Please refer to Quick Start - Implementation to complete the operations of logging in to the room, publishing streams, and playing streams in sequence.

Previous

Video Encoding Format Selection

Next

Implementing Video Call with Vue (Web)

On this page

Back to top