All Products
Search
Document Center

Intelligent Media Services:Configure on-demand publishing and subscription

Last Updated:Jun 16, 2025

ApsaraVideo Real-time Communication (ARTC) SDK for Web allows you to publish and subscribe to audio and video streams on demand. You can determine when to ingest (publish) your audio and video streams to other users and when to receive (subscribe to) the audio and video streams of other users. This provides more flexibility and control for real-time communication and helps to meet specific business requirements. This topic provides the sample code that is used to configure on-demand publishing and subscription.

Note

By default, the SDK lets you automatically publish your stream after you join a channel and automatically subscribe to the streams of remote users in the channel. If you want to publish and subscribe to streams on demand, use the methods that are described in the following sections.

Publish audio and video streams on demand

You can call methods such as publishLocalVideoStream to disable automatic publishing before you call joinChannel to join a channel.

// Make sure that an AliRtcEngine instance is created.
// Disable automatic publishing of video streams.
aliRtcEngine.publishLocalVideoStream(false);
// Disable automatic publishing of audio streams.
aliRtcEngine.publishLocalAudioStream(false);
// Disable automatic publishing of screen-sharing streams.
aliRtcEngine.publishLocalScreenShareStream(false);

After you disable automatic publishing, no stream is ingested even if data is being collected from the camera, microphone, or shared screen. If you want to publish a stream, call one of the preceding methods and set the relevant parameter to true.

Subscribe to audio and video streams on demand

You can call methods such as setDefaultSubscribeAllRemoteAudioStreams to disable automatic subscription before you call joinChannel to join a channel.

// Make sure that an AliRtcEngine instance is created.
// Disable automatic subscription to audio streams.
aliRtcEngine.setDefaultSubscribeAllRemoteAudioStreams(false);
// Disable automatic subscription to video streams and screen-sharing streams.
aliRtcEngine.setDefaultSubscribeAllRemoteVideoStreams(false);

After you disable automatic subscription, call subscribeRemoteMediaStream if you want to subscribe to a stream.

// Use npm to import the AliRtcVideoTrack object.
import { AliRtcVideoTrack } from 'aliyun-rtc-sdk';
// Alternatively, import the object from the window.
// const AliRtcVideoTrack = window.AliRtcEngine.AliRtcVideoTrack;

// In this example, you subscribe to the user after the remoteUserOnLineNotify event that you listen to is triggered.
// In actual scenarios, you can determine whether you want to subscribe to the user based on your requirements.
aliRtcEngine.on('remoteUserOnLineNotify', (userId) => {
  // Subscribe to all streams of the user, including video streams, audio streams, and screen-sharing streams. This way, no error occurs if specific types of streams are not ingested.
  // When the user ingests a stream, you are subscribed to the stream.
  aliRtcEngine.subscribeRemoteMediaStream(
    userId,
    AliRtcVideoTrack.AliRtcVideoTrackBoth,
    true,
    true
  );
});

aliRtcEngine.on('remoteUserOffLineNotify', (userId) => {
  // When the user leaves the channel, you can cancel the subscription.
});