Multi-Room Login
Feature Introduction
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. To use it, please contact ZEGO technical support to enable related 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, implementing more flexible co-hosting business. ZEGO recommends it for cross-room co-hosting and super small class scenarios in online education.
-
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. 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. At the same time, they log in to the small group room to discuss and co-host with students in the same group. The teaching assistant logs in to the small group room to maintain classroom order or answer students' questions.
Sample Source Code Download
Please refer to Download Sample Source Code to get the source code.
For related source code, please check the files in the "/ZegoExpressExample/Examples/Others/MultipleRooms" directory.
Prerequisites
Before implementing multi-room features, please ensure:
-
You have integrated ZEGO Express SDK (version 2.9.0 and above) in the project and implemented basic real-time audio and video features. For details, please refer to Quick Start - Integration and Quick Start - Implementation Flow.
-
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 this room.
- Join the second room. After successful login, you can publish streams, play streams, send and receive real-time messages in this room.
- Leave the first and second rooms. There is no timing restriction on leaving rooms.
The implementation of specific features 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 value of "mode" is "ZegoRoomModeMultiRoom".
If you set multi-room mode without enabling the multi-room feature, logging in to the room will fail and an error will be returned. The error code is "1002036".
// Set room mode to multi-room mode
[ZegoExpressEngine setRoomMode:ZegoRoomModeMultiRoom];2 Initialize SDK
Please refer to "Initialization" in Quick Start - Implementation Flow 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.
[[ZegoExpressEngine sharedEngine] loginRoom:@"multi_room_01" user:[ZegoUser userWithUserID:@"multi_room_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 "streamID". After exiting the specified room, the stream publishing operation corresponding to that room will stop.
ZegoPublisherConfig *config = [[ZegoPublisherConfig alloc] init];
config.roomID = @"multi_room_01";
[[ZegoExpressEngine sharedEngine] startPublishingStream:@"multi_room_publish_01" config:config channel:ZegoPublishChannelMain];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 "streamID". After exiting the specified room, the stream playing operation corresponding to that room will stop.
ZegoPlayerConfig *config = [[ZegoPlayerConfig alloc] init];
config.roomID = @"multi_room_01";
[[ZegoExpressEngine sharedEngine] startPlayingStream:@"multi_room_player_01" canvas:[ZegoCanvas canvasWithView:self.playView] config:config];6 Send/Receive real-time messages
The implementation of sending and receiving real-time messages in multi-room is consistent with single-room. For details, please refer to Common Features - Real-time Messaging.
7 Logout room
Pass in the room ID (roomID) and call the logoutRoom interface to log out of the room.
If you want to log out of all logged-in rooms at once, you can directly call the logoutRoom interface without "roomID".
- In single-room mode, logging out of the room will also stop all publishing and playing streams.
- In multi-room mode, logging out of a room can only log out of the specified room and stop publishing and playing streams in that room.
[[ZegoExpressEngine sharedEngine] logoutRoom:@"multi_room_01"];FAQ
- Whether the setRoomMode interface was called before initializing the SDK. At this time, error code "1001020" will be reported.
- Whether you have contacted ZEGO technical support to enable the multi-room feature.
- Whether correct parameters are passed in.
If multi-room mode is set without enabling the multi-room feature, logging in to the room will fail and error code "1002036" will be returned.
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. For more requirements, please contact ZEGO technical support for extended capabilities.
In multi-room mode, if the passed-in login user information 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.
