Preload channels

Updated

Preloading channels for faster rendering.

This page explains how to implement channel preloading to achieve near-instant channel entry. By preloading channel information and media streams before users join, you can significantly reduce the time required to render the first video frame and create a smoother viewing experience.

The following video demonstrates the channel preload experience:

The following figure illustrates the best practice when preloading channels:

Following these guidelines, you can reduce the first frame loading time for Chrome browsers on Windows and macOS to as low as 300 milliseconds.

Prerequisites

To test the code used in this page you need to have:

  • An Agora account and project.
  • A computer with Internet access. Ensure that no firewall is blocking your network communication.
  • Implemented the SDK quickstart.
  • A supported browser.
  • Physical media input devices, such as a camera and a microphone.
  • A JavaScript package manager such as npm.

Project setup

No additional project setup is required for Web.

Implementation

Follow these steps to implement channel preloading:

Preload channel list

On the channel list page, preload the channels within the user's view area with the preload method.

  • If the total number of channels does not exceed 10, preload all channels.
  • If the total number of channels exceeds 10, preload only the first 10 channels. If the user scrolls the channel list, wait for the scrolling to stop, then preload the new channels in the user's view area.

To speed up the subsequent joining of the channel, use a wildcard token in the token parameter of the preload method.

Pre-join individual channels

Implement pre-joining for different mouse interactions:

  • Before entering the channel

    When a user hovers over a channel, best practice is to join the channel and subscribe to audio and video before entering. The specific operations include the following:

    • Call join to join the channel and subscribe to the host's audio and video streams.
    • Render the video in advance in a small preview window.

    To speed up joining the channel, use a wildcard token in the token parameter of the join method.

  • Entering the channel

    To reduce the time it takes to render the first frame of the host's video, enable subscribing to the host's media stream as soon as the user enters the channel. When calling join, set autoSubscribe to true in options:

    // Create client
    const client = AgoraRTC.createClient({
        mode: 'live',
        codec: 'vp8',
        role: 'host',
    });
    
    // Enables auto subscription when the client joins a channel
    await client.join(APP_ID, CNAME, TOKEN, UID / null, {
        autoSubscribe: true,
    });

    The following table compares the steps from when a user enters the channel to when the host's media stream is rendered, before and after enabling this function:

    BeforeAfter
    1. The user joins the channel.
    2. The channel sends the host/media information.
    3. The user subscribes to the media stream in the channel.
    4. The Agora backend starts sending the media stream in the channel.
    5. The user waits for the Agora backend to send the media stream, receives it, and renders it locally.
    1. The user joins the channel.
    2. The Agora backend immediately sends the media stream in the channel.
    3. The channel sends the host/media information.
    4. After the user subscribes to the media stream, they can immediately receive and render the media stream locally.

    Note

    Joining a channel incurs additional fees due to the transmission of media streams. See Pricing for details.

    When a user clicks on the channel in the list, take the following actions:

    • Switch the app interface from the channel list page to the single channel page. Best practice is to add a page switching animation.
    • Call IRemoteAudioTrack.play and IRemoteVideoTrack.play. The video needs to be passed in the specified DOM element.
  • Leaving without entering

    When a user hovers over a channel but does not click to enter, best practice is to call leave.

Precautions

  • Wildcard tokens can speed up the process of joining a channel but may cause room overflow. Decide whether to use wildcard tokens based on your specific needs.
  • Pre-joining channels incurs additional charges.