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:
-
AgoraRtcConnectionThe
AgoraRtcConnectionobject identifies a connection. It contains the following information:- Channel name
- User ID of the local user
You create multiple
AgoraRtcConnectionobjects, each with a different channel name and user ID. EachAgoraRtcConnectioninstance can independently publish multiple audio streams and a single video stream. -
AgoraRtcEngineKit(Ex)The class contains methods tailored for interacting with a designated
AgoraRtcConnectionobject.To join multiple channels, you call
joinChannelExByTokenmethod in theAgoraRtcEngineKit(Ex)class multiple times, using a differentAgoraRtcConnectioninstance each time.
When joining multiple channels:
-
Ensure that the user ID for each
AgoraRtcConnectionobject is unique and nonzero. -
Configure publishing and subscribing options for the
AgoraRtcConnectionobject injoinChannelExByToken. -
Pass the
AgoraRtcEngineDelegateobject to thedelegateparameter when calling thejoinChannelExByTokenmethod to receive multiple channel-related event notifications.
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.
-
Define an
AgoraRtcConnectionobject in your project'sViewController.swiftfile.let connection1 = AgoraRtcConnection() -
Join the channel using a random user ID.
var mediaOptions = AgoraRtcChannelMediaOptions() mediaOptions.autoSubscribeVideo = .of(true) mediaOptions.autoSubscribeAudio = .of(true) connection1.channelId = channelName1 connection1.localUid = UInt.random(in: 1001...2000) var result = agoraKit.joinChannelEx(byToken: "your token", connection: connection1, delegate: channel1, mediaOptions: mediaOptions, joinSuccess: nil) channel1.channelId = channelName1 channel1.connectionDelegate = self if result != 0 { self.showAlert(title: "Error", message: "joinChannel1 call failed: \(result), please check your params") } -
Set up the remote video in the
didJoinedOfUidcallback.func rtcEngine(_ engine: AgoraRtcEngineKit, channelId: String, didJoinedOfUid uid: UInt, elapsed: Int) { LogUtils.log(message: "remote user join: \(uid) \(elapsed)ms", level: .info) // In this operation, only one remote video view is available // Here, we check if there's a view tagged with that UID let videoCanvas = AgoraRtcVideoCanvas() videoCanvas.uid = uid // The view to be bound videoCanvas.view = channelId == channelName1 ? channel1RemoteVideo.videoView : channel2RemoteVideo.videoView videoCanvas.renderMode = .hidden let connection = AgoraRtcConnection() agoraKit.setupRemoteVideoEx(videoCanvas, connection: connection) }
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 offers the JoinMultiChannels open-source sample project for your reference. Download the project or view the source code for a more detailed example.
