The ApsaraVideo Real-time Communication (ARTC) Web SDK is a toolkit provided by Alibaba Cloud for developing web-based real-time communication applications. It allows you to quickly integrate high-quality features, such as audio/video calls and real-time messaging, into your web applications. This guide shows you how to quickly build your first ARTC application.
Step 1: Create an application
-
Log on to the ApsaraVideo Live console.
-
In the left-side navigation pane, choose .
-
Click Create Application.
-
Enter a custom instance name, select the Terms of Service checkbox, and then click Purchase Now.
-
After the success message appears, refresh the Applications page to view your new ApsaraVideo Real-time Communication application.
NoteCreating an application is free. You are charged on a pay-as-you-go basis for actual usage. For more information, see Billing of audio and video calls.
Step 2: Get application ID and AppKey
After you create the application, find it in the application list. In the Actions column, click Manage to open the Basic Information page. On this page, find the Application ID and AppKey.
Step 3: Integrate the SDK
-
Integrate the SDK.
Script
In your HTML page, include the SDK script.
<script src="https://g.alicdn.com/apsara-media-box/imp-web-rtc/7.1.9/aliyun-rtc-sdk.js"></script>NPM
In your project, run the following command to install the SDK.
npm install aliyun-rtc-sdk --save -
Initialize the engine.
// Choose one of the following two import methods. // Use this if you are importing from an npm package. import AliRtcEngine from 'aliyun-rtc-sdk'; // Use this if you are including the SDK with a script tag. const AliRtcEngine = window.AliRtcEngine; // Check browser compatibility. const checkResult = await AliRtcEngine.isSupported(); if (!checkResult.support) { // The current environment is not supported. Prompt the user to switch to or upgrade their browser. } // Create an engine instance. You can save it as a global variable. const aliRtcEngine = AliRtcEngine.getInstance(); -
After creating the AliRtcEngine instance, listen for and handle relevant events.
// Fired when the local user leaves the channel. aliRtcEngine.on('bye', (code) => { // `code` is a reason code. For details, see the API reference. console.log(`bye, code=${code}`); // Handle your business logic here, such as exiting the call page. }); // Fired when a remote user comes online. aliRtcEngine.on('remoteUserOnLineNotify', (userId, elapsed) => { console.log(`User ${userId} joined the channel in ${elapsed} seconds.`); // Handle your business logic here, such as displaying the UI module for this user. }); // Fired when a remote user goes offline. aliRtcEngine.on('remoteUserOffLineNotify', (userId, reason) => { // `reason` is a reason code. For details, see the API reference. console.log(`User ${userId} left the channel. Reason code: ${reason}`); // Handle your business logic here, such as destroying the UI module for this user. }); // Fired when the subscription state of a remote stream changes. aliRtcEngine.on('videoSubscribeStateChanged', (userId, oldState, newState, interval, channelId) => { // 'oldState' and 'newState' are AliRtcSubscribeState values. // Values: 0 (initialized), 1 (unsubscribed), 2 (subscribing), 3 (subscribed). // `interval` is the time between state changes, in milliseconds. console.log(`Subscription state of remote user ${userId} in channel ${channelId} changed from ${oldState} to ${newState}.`); // Handle the logic for viewing the remote stream here. // When `newState` becomes 3, you can play the remote stream by calling setRemoteViewConfig. // When `newState` becomes 1, you can stop the playback. }); // Fired when the authentication information expires. aliRtcEngine.on('authInfoExpired', () => { // This callback indicates that the authentication information has expired. // Get a new token and other data, then call the refreshAuthInfo method to update the authentication data. aliRtcEngine.refreshAuthInfo({ userId, token, timestamp }); }); // Fired when the authentication information is about to expire. aliRtcEngine.on('authInfoWillExpire', () => { // This callback is fired 30 seconds before expiration. You should update the authentication information promptly. // To stay in the session, get a new token and other data, and call joinChannel to rejoin the channel. }); -
(Optional) Set the channel mode. The default is communication mode. For more information, see Set the channel mode and user role.
// Set the channel mode. Valid values: 'communication' (communication mode), 'interactive_live' (interactive mode). aliRtcEngine.setChannelProfile('interactive_live'); // Set the user role. This method is effective only in interactive mode. // Valid values: 'interactive' (streamer, can publish and subscribe to streams), 'live' (viewer, can only subscribe to streams). aliRtcEngine.setClientRole('interactive'); -
Join a channel. For information on how to generate a token, see token-based authentication. You can choose to join with a single parameter or multiple parameters based on your needs.
-
Join with a single parameter
const userName = 'Test User 1'; // You can change this to your username. Chinese characters are supported. try { // You need to implement fetchToken to get the Base64-encoded token from your server. const base64Token = await fetchToken(); await aliRtcEngine.joinChannel(base64Token, userName); // Joined the channel successfully. Proceed with other operations. } catch (error) { // Failed to join the channel. } -
Join with multiple parameters
// Generate authentication information on your server or locally by following the token-based authentication guide. // IMPORTANT: For data security, never publish the token calculation logic that includes your AppKey to end users. const appId = 'yourAppId'; // Get this from the console. const appKey = 'yourAppKey'; // Get this from the console. Do not expose your AppKey in a production environment. const channelId = 'AliRtcDemo'; // You can change this to your channel ID. Only letters and digits are supported. const userId = 'test1'; // You can change this to your user ID. Only letters and digits are supported. const userName = 'Test User 1'; // You can change this to your username. Chinese characters are supported. const timestamp = Math.floor(Date.now() / 1000) + 3600; // Expires in one hour. try { const token = await generateToken(appId, appKey, channelId, userId, timestamp); // Join the channel. Parameters like token and timestamp are typically returned from the server. // Note: When calling this method, ensure the channelId, userId, appId, and timestamp parameters match those used to generate the token. await aliRtcEngine.joinChannel({ channelId, userId, appId, token, timestamp, }, userName); // Joined the channel successfully. Proceed with other operations. } catch (error) { // Failed to join the channel. }
-
-
Follow these steps to preview your local video. By default, after you join a channel, local audio and video data is automatically captured and published to the Global Realtime Transport Network (GRTN).
-
In the HTML code, add a VIDEO element with an
idoflocalPreviewer.<video id="localPreviewer" muted style="display: block;width: 320px;height: 180px;background-color: black;" ></video> -
Call the
setLocalViewConfigmethod and pass the element ID to start the preview.// The first parameter accepts an HTMLVideoElement or its ID. Pass null to stop the preview. // The second parameter specifies the stream type: 1 for a camera stream, 2 for a screen sharing stream. aliRtcEngine.setLocalViewConfig('localPreviewer', 1);
-
-
Subscribe to remote audio and video streams. By default, after joining a channel, the SDK automatically subscribes to the audio and video streams of other streamers. Audio streams are played automatically. To view a camera stream or screen sharing stream, call the
setRemoteViewConfigmethod.-
In the HTML code, add a
DIVelement with anidofremoteVideoContaineras a container.<div id="remoteVideoContainer"></div> -
Listen for subscription changes to remote video streams. When a stream is subscribed, play it by calling the
setRemoteViewConfigmethod. When it is unsubscribed, remove the video element.// Store Video elements. const remoteVideoElMap = {}; // The remote container element. const remoteVideoContainer = document.querySelector('#remoteVideoContainer'); function removeRemoteVideo(userId) { const el = remoteVideoElMap[userId]; if (el) { aliRtcEngine.setRemoteViewConfig(null, userId, 1); el.pause(); remoteVideoContainer.removeChild(el); delete remoteVideoElMap[userId]; } } // This is the same example as in the "listen for and handle relevant events" step for `videoSubscribeStateChanged`. aliRtcEngine.on('videoSubscribeStateChanged', (userId, oldState, newState, interval, channelId) => { // `oldState` and `newState` are of the AliRtcSubscribeState type. // Values: 0 (initialized), 1 (unsubscribed), 2 (subscribing), 3 (subscribed). // `interval` is the time between state changes, in milliseconds. console.log(`Subscription state of remote user ${userId} in channel ${channelId} changed from ${oldState} to ${newState}.`); // Example handler if (newState === 3) { const video = document.createElement('video'); video.autoplay = true; video.setAttribute('style', 'display: block;width: 320px;height: 180px;background-color: black;'); remoteVideoElMap[userId] = video; remoteVideoContainer.appendChild(video); // The first parameter is an HTMLVideoElement. // The second parameter is the remote user ID. // The third parameter specifies the stream type: 1 for a camera stream, 2 for a screen sharing stream. aliRtcEngine.setRemoteViewConfig(video, userId, 1); } else if (newState === 1) { removeRemoteVideo(userId); } });
-
-
End the session and clean up resources.
// Stop the local preview. await aliRtcEngine.stopPreview(); // Leave the channel. await aliRtcEngine.leaveChannel(); // Destroy the instance to release resources. aliRtcEngine.destroy();
Quick start demo
The JavaScript in this demo includes a generateToken method for calculating a token. For security reasons, never publish this code or your AppKey in a client-side JavaScript file, as this can lead to information leaks and abuse. We recommend that you perform token signing on your server and retrieve the token through an authenticated API on the client.
Prerequisites
This demo requires an HTTP server in your development environment. If you do not have the http-server npm package, run npm install --global http-server to install it globally.
Step 1: Create the directory
Create a demo folder containing two files: quick.html and quick.js.
- demo
- quick.html
- quick.js
Step 2: Edit quick.html
Copy the following code into quick.html and save the file.
Step 3: Edit quick.js
Copy the following code into quick.js. Paste your application ID and AppKey into the specified variables and save the file.
Step 4: Run the demo
-
In your terminal, navigate to the
demofolder and runhttp-server -p 8080to start an HTTP server. -
Open a new browser tab and navigate to
localhost:8080/quick.html. Enter a Channel ID and a User ID, then click Join Channel. -
Open a second browser tab and navigate to
localhost:8080/quick.html. Enter the same Channel ID but a different User ID, then click Join Channel. -
Verify that the media stream from the other user is automatically subscribed to and displayed on the page.