Login to Multiple Rooms
Feature Overview
The same user can join multiple rooms at the same time, and simultaneously publish streams, play streams, send real-time messages, and receive message callbacks in multiple rooms.
- ZEGO Express SDK has supported this feature since version 2.9.0.
- This feature is not enabled by default. If you need to use it, please contact ZEGO Technical Support to enable relevant permissions.
- After this feature is enabled, it supports joining up to 5 rooms at the same time by default. If you have more requirements, please contact ZEGO Technical Support for assistance.
Application Scenarios
This feature can isolate messages and callbacks of multiple rooms, achieving more flexible co-hosting business. ZEGO recommends using it for cross-room co-hosting and super small class scenarios in online education.
-
Cross-room Co-hosting
Streamer A and Streamer B perform cross-room co-hosting. Streamer A can notify all viewers in Room A to log in to Streamer B's room to play streams. Streamer B can similarly notify all viewers in Room B to log in to Streamer 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. At the same time, they log in to the group room to discuss and co-host with students in the same group. The teaching assistant logs in to the group room to maintain classroom order or answer student questions.
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 "/ZegoExpressExample/Others/src/main/java/im/zego/others/multiplerooms" directory.
Prerequisites
Before implementing multi-room functionality, please ensure:
-
You have integrated ZEGO Express SDK (version 2.9.0 and above) in your project and implemented basic real-time audio and video functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
-
You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign.
-
You have contacted ZEGO Technical Support to enable the multi-room feature.
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 on 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 use the ZegoRoomMode class to set the room mode to multi-room mode, that is, the "mode" value is "MULTI_ROOM".
If you set multi-room mode without enabling the multi-room feature, logging in to the room will fail with an error, returning error code "1002036".
// Set room mode to multi-room mode
ZegoExpressEngine.setRoomMode(ZegoRoomMode.MULTI_ROOM)2 Initialize SDK
Please refer to "Initialization" in Quick Start - Implementation to initialize the SDK.
3 Login to 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 another room, you will be prompted that you are already 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
ZegoUser user = new ZegoUser("user1");
// Start logging in to room
engine.loginRoom("room1", user);4 Publish Stream
Pass in parameters such as stream ID (streamID) and room ID (roomID), and call the startPublishingStream (with "ZegoPublisherConfig" parameter) interface to publish stream in the specified room.
In multi-room mode, you must use ZegoPublisherConfig to specify the "roomID" associated with "streamID". After exiting the specified room, the stream publishing operation for that room will be stopped.
ZegoPublisherConfig config = new ZegoPublisherConfig();
config.roomID = "room1";
// Start publishing stream
engine.startPublishingStream("stream1", config, ZegoPublishChannel.MAIN);5 Play Stream
Pass in parameters such as stream ID (streamID) and room ID (roomID), and call the startPlayingStream (with "ZegoPlayerConfig" parameter) interface to play stream in the specified room.
In multi-room mode, you must use ZegoPlayerConfig to specify the "roomID" associated with "streamID". After exiting the specified room, the stream playing operation for that room will be stopped.
ZegoPlayerConfig config = new ZegoPlayerConfig();
config.roomID = "room1";
// The following play_view is a SurfaceView/TextureView/SurfaceTexture object on the UI interface
engine.startPlayingStream("stream1", new ZegoCanvas(play_view), config);6 Send/Receive Real-Time Messages
The implementation of sending and receiving real-time messages in multi-room and single-room is the same. For details, please refer to Common Features - 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 exit all logged-in rooms at once, you can directly call the logoutRoom interface without "roomID".
- In single-room mode, exiting the room will stop all stream publishing and playing at the same time.
- In multi-room mode, exiting the room can only exit the specified room and stop stream publishing and playing for that room.
// Logout room
engine.logoutRoom("room1");FAQ
- Whether the setRoomMode interface was called before initializing the SDK. In this case, error code "1001020" will be reported.
- Whether you have contacted ZEGO Technical Support to enable the multi-room feature.
- Whether the correct parameters were passed in.
When the number of rooms logged in at the same time 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 ZEGO Technical Support for extended capabilities.
In multi-room mode, if the logged-in user information passed in is not the same, error code "1002018" will be returned.
In multi-room mode, you must call the startPublishingStream interface with the "ZegoPublisherConfig" parameter to publish stream, otherwise error code "1003070" will be returned.
In multi-room mode, you must call the startPlayingStream interface with the "ZegoPlayerConfig" parameter to play stream, otherwise error code "1004070" will be returned.
