Join multiple channels
Updated
Broadcast or subscribe to multiple channels.
Agora Video SDK enables you to simultaneously join multiple channels. This capability allows you to receive and publish audio and video streams across multiple channels concurrently.
Understand the tech
Video SDK's multi-channel functionality is based on two key components:
-
RtcConnectionThe
RtcConnectionobject identifies a connection. It contains the following information:- Channel name
- User ID of the local user
You create multiple
RtcConnectionobjects, each with a different channel name and user ID. EachRtcConnectioninstance can independently publish multiple audio streams and a single video stream. -
IRtcEngineExThe class contains methods tailored for interacting with a designated
RtcConnectionobject.To join multiple channels, you call
joinChannelExmethod in theIRtcEngineExclass multiple times, using a differentRtcConnectioninstance each time.
When joining multiple channels:
-
Ensure that the user ID for each
RtcConnectionobject is unique and nonzero. -
Configure publishing and subscribing options for the
RtcConnectionobject injoinChannelEx. -
Use the
connectionparameter in each callback to identify the corresponding channel.
Prerequisites
Ensure that you have implemented the SDK quickstart in your project.
Implementation
This section explains how to join a second channel as a host after you have already joined the first channel.
- Join the second channel
Call joinChannelEx to join a secondary channel by specifying the RtcConnection object and using a random user ID.
let token2 = "token2";
const channelId2 = "channel2";
let uid2 = 321;
// Join multiple channels
rtcEngine.joinChannelEx(
token2,
// RtcConnection
{
channelId: channelId2,
localUid: uid2,
},
// ChannelMediaOptions
{
clientRoleType: ClientRoleType.ClientRoleBroadcaster,
publishMicrophoneTrack: false,
publishCameraTrack: false,
autoSubscribeAudio: false,
autoSubscribeVideo: false,
publishSecondaryCameraTrack: true,
}
);- Set up remote user video
Listen to the onUserJoined callback and set the remote video.
// Register callback for when a remote user joins the current channel
agoraEngine.registerEventHandler(
{
onUserJoined: (_connection: RtcConnection, uid: number) => {
showMessage("remote user " + uid + " added");
setRemoteUid(uid);
},
}
);
// Creating a Remote View Using RtcSurfaceView
<RtcSurfaceView
canvas={
{
uid: remoteUid,
sourceType: VideoSourceType.VideoSourceRemote,
}
}
style={styles.videoView}
/>;Reference
This section contains content that completes the information on this page, or points you to documentation that explains other aspects to this product.
Sample project
Agora provides the JoinMultipleChannel open-source sample project for your reference. Download the project or view the source code for a more detailed example.
