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:

  • RtcConnection

    The RtcConnection object identifies a connection. It contains the following information:

    • Channel name
    • User ID of the local user

    You create multiple RtcConnection objects, each with a different channel name and user ID. Each RtcConnection instance can independently publish multiple audio streams and a single video stream.

  • IRtcEngineEx

    The class contains methods tailored for interacting with a designated RtcConnection object.

    To join multiple channels, you call joinChannelEx method in the IRtcEngineEx class multiple times, using a different RtcConnection instance each time.

When joining multiple channels:

  • Ensure that the user ID for each RtcConnection object is unique and nonzero.

  • Configure publishing and subscribing options for the RtcConnection object in joinChannelEx.

  • Use the connection parameter 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.

  1. 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,
  }
);
  1. Set up remote user video

Listen to the onUserJoined callback and set up the remote video.

const EventHandles = {
  // Monitor remote user joining channel events
  onUserJoined: ({ channelId, localUid }, remoteUid, elapsed) => {
    console.log('Remote user ' + remoteUid + ' joined');
    // After the remote user joins the channel, set the remote video window
    rtcEngine.setupRemoteVideoEx(
      {
        sourceType: VideoSourceType.VideoSourceRemote,
        uid: remoteUid,
        view: remoteVideoContainer,
        setupMode: VideoViewSetupMode.VideoViewSetupAdd,
      },
      {
        channelId: channelId2,
        localUid: uid2,
      },
    );
  },
  // Other event callbacks
};

// Register event callback
rtcEngine.registerEventHandler(EventHandles);

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.

API reference