Web

Updated at:

This topic describes the basic features of DingRTC, which include initializing the software development kit (SDK), joining a channel, publishing local streams, subscribing to remote streams, and leaving a channel.

Prerequisites

Terms

When you use the DingRTC Web SDK, you will encounter the following basic concepts:

  • The DingRTCClient class. An instance of this class represents a local client. Its methods provide features such as joining and leaving a channel, and publishing and subscribing to audio and video streams.

  • The LocalTrack and RemoteTrack classes. Instances of these classes, and of other classes that use them as a base class, represent audio or video objects. These objects provide control operations for audio and video streams, such as playback and pause.

Basic logic

  1. Call the createClient() method to create a client instance.

  2. Call join() to join a Real-Time Communication (RTC) channel.

  3. Publish local audio and video tracks.

Create a camera track by calling createCameraVideoTrack() or a microphone track by calling createMicrophoneAudioTrack().

After joining the channel, call publish() with the audio and video track objects to publish them to the channel.

  1. Subscribe to remote audio and video tracks.

Before joining the channel, add a listener for the "user-published" event to receive notifications when remote users publish streams. The callback parameters include the publisher's information and the type of audio or video track.

The remoteUsers property of the client instance provides a list of all remote users in the channel. You can use this property to subscribe to audio and video tracks that were published before you joined the channel.

Call subscribe() to subscribe to a remote user. After the subscription is successful, retrieve the remote audio and video objects from the API response or from the RemoteUser object.

Call play() to play the remote audio and video tracks.

Procedure

Note

The implementation methods in this topic are for reference only. You can develop your application as needed. The sample code uses ES Module imports as an example. If you use the SDK in UMD mode, see the "Integrate the client SDK" section to replace the SDK entry object.

  1. Create a Client object.

    import DingRTC from 'dingrtc';
    
    const client = DingRTC.createClient();
  2. Join a channel.

    await client.join({
     	appId: joinInfo.appId,
     	token: joinInfo.token,
     	uid: joinInfo.uid,
     	channel: joinInfo.channel,
     	userName: joinInfo.userName,
    });

    Information required to join a channel:

    Property

    Type

    Description

    appId

    string

    The AppId of your DingRTC project. It can contain only uppercase letters, lowercase letters, digits, and underscores (_).

    channel

    string

    The channel ID. It can contain only characters in the set [A-Za-z0-9_-] and must be no more than 64 characters in length.

    token

    string

    The token for channel authentication.

    uid

    string

    The user ID. It can contain only characters in the set [A-Za-z0-9_-] and must be no more than 64 characters in length.

    Note

    If another client logs on with the same user ID, the client that joined the channel first is kicked out.

    userName

    string

    The username. The length cannot exceed 64 bytes in UTF-8 encoding.

  3. Publish or unpublish local audio and video tracks.

    • Create local audio and video tracks

      // Camera track
      const cameraTrack = await DingRTC.createCameraVideoTrack({
        frameRate: 15,
        dimension: 'VD_1280x720',
      });
      // Microphone track
      const micTrack = await DingRTC.createMicrophoneAudioTrack();
      Note

      After the tracks are created, you can call the play method to start a local preview, as shown in the following example.

      Note:

      • The play method for a video track accepts a DOM element as a parameter. The SDK then creates a video element within this DOM element to play the video track. You do not need to create the video element yourself. The sample code specifies the ID of the DOM element.

      • You do not typically need to play the audio from your own microphone. This example is for API demonstration purposes only, such as in special scenarios like device diagnostics.

      cameraTrack.play('#local');
      // micTrack.play();
    • Publish the local audio and video tracks

      client.publish([cameraTrack, micTrack]);
    • Unpublish the local audio and video tracks

      client.unpublish([cameraTrack, micTrack]);
  4. Subscribe to or unsubscribe from remote streams.

    • Subscribe to remote audio and video

      Before joining the channel, add a listener for the "user-published" event on the Client object. This ensures you do not miss notifications when remote users publish audio or video tracks. In the callback for this event, you can call subscribe() to subscribe to the remote user's audio and video tracks.

      client.on('user-published', (user, mediaType, auxiliary) => {
        if (mediaType === 'video') {
          client.subscribe(user.userId, mediaType, auxiliary).then((track) => {
            track.play(`#uid${user.userId}`);
          });
          // Here, mcuSubscribed is an assumed variable that indicates whether you have subscribed to the mixed MCU audio stream.
        } else if (!mcuSubscribed) {
          mcuSubscribed = true;
          client.subscribe('mcu', 'audio').then((track) => {
            track.play();
          });
        }
      });

      Alternatively, you can use the remoteUsers property of the client instance to subscribe to the audio and video tracks published by a specific remote user.

      for (const user of client.remoteUsers) {
        if (user.hasVideo) {
          client.subscribe(user.userId, 'video').then((track) => {
            track.play(`#uid${user.userId}`);
          });
        }
        if (user.hasAudio && !mcuSubscribed) {
          mcuSubscribed = true;
          client.subscribe('mcu', 'audio').then((track) => {
            track.play();
          });
        }
      }
      Note

      Subscribing to the audio stream of a specific user is not yet supported. To subscribe to an audio stream, set userId to the string 'mcu'. This means you are subscribing to the mixed audio stream of all remote users in the channel. You only need to subscribe to the mixed audio stream once. When a new remote user publishes an audio stream, it is automatically mixed into the subscribed MCU audio stream.

    • Unsubscribe from remote audio and video

      client.unsubscribe(user.userId, 'video'); // Unsubscribe from the user's video track
      client.unsubscribe('mcu', 'audio'); // Unsubscribe from the mixed remote audio track
      Note

      Unsubscribing from the audio stream of a specific user is not yet supported. To unsubscribe from an audio stream, set userId to the string 'mcu'. This means you are unsubscribing from the mixed audio stream of all remote users in the channel. You only need to unsubscribe from the mixed audio stream once.

    • Subscribe to video streams of different resolutions

      You can subscribe to video streams in high or low resolution. The default is high resolution. To switch a specific user's video track to a low-definition stream, call the following API:

      client.setRemoteVideoStreamType(user.userId, 'low');

      Alternatively, you can modify the global default subscription resolution:

      client.setRemoteDefaultVideoStreamType('low');
      Note
      1. This API call affects only users who join the channel after the call is made. It does not affect users who are already in the channel.

      2. In Web SDK v3.3.0 and later, four new subscription resolutions are available for the two preceding APIs: FHD, HD, SD, and LD. For more information, see RemoteStreamType. The legacy 'high' and 'low' values are retained and correspond to FHD and LD, respectively. The default value is 'high' or FHD, which represents the highest resolution published by the remote user. For example, if the remote user publishes a 1080p stream, 'FHD' or 'high' means the subscription resolution is 1080p. If the remote user publishes a 480p stream, 'FHD' or 'high' means the subscription resolution is 480p.

  5. Leave the channel.

    client.leave();

This API call affects remote users who join the channel after the call is made. It does not affect remote users who are already in the channel.

What to do next

You can download the sample code and run the demo to quickly implement a real-time audio and video call. For more information, see Run the Web Demo.