preloadChannel

Preloads a channel with token, channelId, and uid.

 virtual int preloadChannel(const char* token, const char* channelId, uid_t uid) = 0;

Details

Since
v4.2.2

When audience members need to switch between different channels frequently, calling the method can help shortening the time of joining a channel, thus reducing the time it takes for audience members to hear and see the host. As it may take a while for the SDK to preload a channel, Agora recommends that you call this method as soon as possible after obtaining the channel name and user ID to join a channel.

Note:
  • When calling this method, ensure you set the user role as audience and do not set the audio scenario as AUDIO_SCENARIO_CHORUS, otherwise, this method does not take effect.
  • You also need to make sure that the channel name, user ID and token passed in for preloading are the same as the values passed in when joinning the channel, otherwise, this method does not take effect.
  • One IRtcEngine instance supports preloading 20 channels at most. When exceeding this limit, the latest 20 preloaded channels take effect.
  • Failing to preload a channel does not mean that you can't join a channel, nor will it increase the time of joining a channel.

If you join a preloaded channel, leave it and want to rejoin the same channel, you do not need to call this method unless the token for preloading the channel expires.

Parameters

token
The token generated on your server for authentication. See .
When the token for preloading channels expires, you can update the token based on the number of channels you preload.
  • When preloading one channel, calling this method to pass in the new token.
  • When preloading more than one channels:
    • If you use a wildcard token for all preloaded channels, call updatePreloadChannelToken to update the token.
      Note: When generating a wildcard token, ensure the user ID is not set as 0. See Secure authentication with tokens.
    • If you use different tokens to preload different channels, call this method to pass in your user ID, channel name and the new token.
channelId
The channel name that you want to preload. This parameter signifies the channel in which users engage in real-time audio and video interaction. Under the premise of the same App ID, users who fill in the same channel ID enter the same channel for audio and video interaction. The string length must be less than 64 bytes. Supported characters (89 characters in total):
  • All lowercase English letters: a to z.
  • All uppercase English letters: A to Z.
  • All numeric characters: 0 to 9.
  • Space
  • "!", "#", "$", "%", "&", "(", ")", "+", "-", ":", ";", "<", "=", ".", ">", "?", "@", "[", "]", "^", "_", "{", "}", "|", "~", ","
uid
The user ID. This parameter is used to identify the user in the channel for real-time audio and video interaction. You need to set and manage user IDs yourself, and ensure that each user ID in the same channel is unique. This parameter is a 32-bit unsigned integer. The value range is 1 to 232-1. If the user ID is not assigned (or set to 0), the SDK assigns a random user ID and returns it in the onJoinChannelSuccess callback. Your application must record and maintain the returned user ID, because the SDK does not do so.

Returns

  • 0: Success.
  • < 0: Failure.
    • -7: The IRtcEngine object has not been initialized. You need to initialize the IRtcEngine object before calling this method.
    • -102: The channel name is invalid. You need to pass in a valid channel name and join the channel again.