Login to Multiple Rooms
Feature Overview
The same user can join multiple rooms simultaneously, and publish streams, play streams, send real-time messages, and receive message callbacks in multiple rooms at the same time.
- ZEGO Express SDK has supported this feature since version 1.3.0.
- This feature is disabled by default. To use it, please contact ZEGOCLOUD Technical Support to enable the relevant permissions.
- After this feature is enabled, it supports joining up to 5 rooms simultaneously by default. If you have more requirements, please contact ZEGOCLOUD Technical Support for assistance.
Application Scenarios
This feature can isolate messages and callbacks from multiple rooms, enabling more flexible co-hosting services. ZEGO recommends using it for cross-room co-hosting and online education super small class scenarios.
- Cross-room Co-hosting
Host A and Host B conduct cross-room co-hosting. Host A can notify all viewers in room A to log in to host B's room to play streams, and Host B can similarly notify all viewers in room B to log in to host A's room to play streams.
- Super Small Class
The teacher enters the large class room to publish streams, and all students log in to the large class room to play streams, while also logging in to group rooms to discuss and co-host with students in the same group. The teaching assistant logs in to group rooms to maintain classroom order or answer students' questions.
Prerequisites
Before implementing multi-room functionality, please ensure:
- You have created a project in the ZEGOCLOUD Console and applied for valid AppID and AppSign.
- You have integrated ZEGO Express SDK (version 2.9.0 and above) in your project and implemented basic audio and video streaming functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementing Video Call.
- You have contacted ZEGOCLOUD Technical Support to enable multi-room functionality.
Usage Steps
Taking the same user joining two rooms as an example, the overall implementation flow is as follows:
- Before initializing the SDK, set the room mode to multi-room.
- Initialize the SDK.
- Join the first room. After successful login, you can publish streams, play streams, send and receive real-time messages in that room.
- Join the second room. After successful login, you can publish streams, play streams, send and receive real-time messages in that room.
- Leave the first and second rooms. There is no timing restriction for leaving rooms.
The implementation of specific functions is as follows:
1 Set Multi-Room Mode
Before initializing the SDK, you must call the setRoomMode interface and set the room mode to multi-room mode through the ZegoRoomMode class, that is, the "mode" value is "MultiRoom".
If you set multi-room mode without enabling multi-room functionality, logging in to the room will fail and an error will be returned with error code "1002036".
// Set room mode to multi-room mode
ZegoExpressEngine.setRoomMode(ZegoRoomMode.MultiRoom)2 Initialize SDK
Please refer to Quick Start - Implementation Flow "Initialization" to initialize the SDK.
3 Login Room
Pass in parameters such as room ID (roomID) and call the loginRoom interface to log in to the room.
- In single-room mode, you can only log in to one room at the same time. When calling the loginRoom interface, if you have already logged in to other rooms, you will be prompted that you have logged in to the same room.
- In multi-room mode, you can log in to multiple rooms at the same time, and the logged-in user information must be the same.
// Create user
let user = {userID: "userID1", userName: "userName1"};
// Start logging in to room
ZegoExpressEngine.instance().loginRoom("room1", user);4 Publish Stream
Pass in parameters such as stream ID (streamID) and room ID (roomID), and call the startPublishingStream interface (with "ZegoPublisherConfig" parameter) to publish stream in the specified room.
In multi-room mode, you must use ZegoPublisherConfig to specify the "roomID" associated with the "streamID". After exiting the specified room, the stream publishing operation corresponding to that room will be stopped.
let config = new ZegoPublisherConfig();
config.roomID = "room1";
// Start publishing stream
ZegoExpressEngine.instance().startPublishingStream("stream1", ZegoPublishChannel.Main, config);5 Play Stream
Pass in parameters such as stream ID (streamID) and room ID (roomID), and call the startPlayingStream interface (with "ZegoPlayerConfig" parameter) to play stream in the specified room.
In multi-room mode, you must use ZegoPlayerConfig to specify the "roomID" associated with the "streamID". After exiting the specified room, the stream playing operation corresponding to that room will be stopped.
let config = new ZegoPlayerConfig();
config.roomID = "room1";
// Start playing stream
ZegoExpressEngine.instance().startPlayingStream("stream1", ZegoPublishChannel.Main, config);6 Send/Receive Real-time Messages
The implementation of sending and receiving real-time messages in multi-room mode is the same as in single-room mode. For details, please refer to Room Real-time Messages.
7 Logout Room
Pass in the room ID (roomID) and call the logoutRoom interface to leave the room.
If you want to leave all logged-in rooms at once, you can directly call the logoutRoom interface without passing parameters.
- In single-room mode, leaving a room will stop all publishing and playing streams.
- In multi-room mode, leaving a room can only exit the specified room and stop the publishing and playing streams of that room.
// Leave room
ZegoExpressEngine.instance().logoutRoom("room1");FAQ
- What is the reason for the failure when calling the setRoomMode interface?
- Whether the setRoomMode interface was called before initializing the SDK. At this time, error code "1001020" will be reported.
- Whether ZEGOCLOUD Technical Support has been contacted to enable multi-room functionality.
- Whether correct parameters are passed in.
- What is the reason for the login failure when calling loginRoom with error code "1002036"?
If multi-room mode is set without enabling multi-room functionality, logging in to the room will fail and error code "1002036" will be returned.
- What is the reason for the login failure when calling loginRoom with error code "1002037"?
When the number of simultaneously logged-in rooms exceeds the maximum number of rooms, error code "1002037" will be returned. Currently, it supports joining up to 5 rooms at the same time by default. If you have more requirements, please contact ZEGOCLOUD Technical Support for extended capabilities.
- What is the reason for the login failure when calling loginRoom with error code "1002018"?
In multi-room mode, if the logged-in user information passed in is not the same, error code "1002018" will be returned.
- What is the reason for the publishing stream failure when calling startPublishingStream with error code "1003070"?
In multi-room mode, you must call the startPublishingStream interface with the "ZegoPublisherConfig" parameter to publish streams. Otherwise, error code "1003070" will be returned.
- What is the reason for the playing stream failure when calling startPlayingStream with error code "1004070"?
In multi-room mode, you must call the startPlayingStream interface with the "ZegoPlayerConfig" parameter to play streams. Otherwise, error code "1004070" will be returned.
