Quickly integrate the CommsEase Whiteboard SDK and publish a whiteboard stream.
Activate the whiteboard service
You can activate the CommsEase whiteboard service in two ways:
-
Activate the Interactive Whiteboard service on the CommsEase official website.
-
If your Alibaba Cloud Real-Time Communication (ARTC) spending is expected to exceed 50,000 CNY per month, you can submit a ticket or send an email to request that Alibaba Cloud purchase the CommsEase Interactive Whiteboard service on your behalf. You pay the whiteboard fees directly to Alibaba Cloud. For more information about how to submit a ticket, see Contact us.
Integrate the whiteboard SDK
Import SDK files
Download and decompress the SDK zip package. Copy the WhiteBoardSDK.js, ToolCollection.js, and pptRenderer.js files from the g2 folder to your project's static files folder (usually the `public` folder) or publish them to your CDN. Then, import these three JS files into your entry HTML file using <script> tags.
Get the Interactive Whiteboard AppKey and AppSecret
-
On the homepage of the CommsEase console, find your application in the Application Management section and click the Application Name.
-
In the Application Configuration navigation bar, click the AppKey Management tab.
-
View and record the AppKey and AppSecret for the application.
For more information about Interactive Whiteboard configurations, see the Interactive Whiteboard Quick Start Guide for Novices.
If Alibaba Cloud purchases the service on your behalf, Alibaba Cloud provides you with the AppKey and AppSecret after the purchase is complete.
Develop the getAuthInfo interface
Add the whiteboard SDK's appkey and appSecret to your server-side configuration parameters. Develop a getAuthInfo interface that performs SHA-1 encryption on data, such as appSecret and curTime, and returns the relevant data to the frontend.
The local example performs encryption on the web page for demonstration purposes only. Do not expose your appSecret in a production environment.
Initialization
Add a DIV element in your HTML code to serve as the whiteboard container.
<div id="whiteboard"></div>
Create a whiteboard SDK instance and join a channel with the `joinRoom` interface.
const appKey = 'Your AppKey'; // You can get this from AppKey Management for your application in the CommsEase console.
const appSecret = 'Your AppSecret';
const nickname = 'Your Nickname';
const uid = 123123; // A positive integer less than Number.MAX_SAFE_INTEGER. If the same UID logs on from multiple locations, previous sessions will be disconnected. If you need multi-device synchronization, use different UIDs for each device.
/**
* This function returns the authentication information required by the Interactive Whiteboard application.
* The Interactive Whiteboard SDK calls this function when needed. The function passes the auth info to the SDK through a promise.
*
* The following code is for demonstration only. In actual development, do not store the appSecret on the client. This could lead to the appSecret being stolen. In actual development, you can use getAuthInfo to request the auth message from your server and then return the auth info to the SDK in the promise.
*/
function getAuthInfo() {
const Nonce = 'xxxx'; // A random string of any length less than 128 characters.
const curTime = Math.round((Date.now() / 1000)); // The current UTC timestamp in seconds since January 1, 1970, 00:00:00.
const checksum = sha1(appSecret + Nonce + curTime);
return Promise.resolve({
nonce: Nonce,
checksum: checksum,
curTime: curTime,
});
}
const sdk = WhiteBoardSDK.getInstance({
appKey: appKey,
nickname: nickname, // Optional
uid: uid,
container: document.getElementById('whiteboard'),
platform: 'web',
record: false, // Specifies whether to enable recording.
getAuthInfo: getAuthInfo,
});
// A string of your choice for the channel. Different clients must join the same channel to communicate.
const channel = '821937123'
sdk.joinRoom({
channel: channel,
createRoom: true
})
.then((drawPlugin) => {
// Allow editing.
drawPlugin.enableDraw(true)
// Set the brush color.
drawPlugin.setColor('rgb(243,0,0)')
// Initialize the toolbar.
const toolCollection = ToolCollection.getInstance({
/**
* The toolbar container. It should be the same as the whiteboard container.
*
* Note that the child elements inside the toolbar are absolutely positioned. Therefore, the container outside the toolbar should have its position set to relative, absolute, or fixed.
* This ensures that the toolbar is displayed correctly inside the container.
*/
container: document.getElementById('whiteboard'),
handler: drawPlugin,
options: {
platform: 'web',
}
});
// Show the toolbar.
toolCollection.show();
});
Load documents
The whiteboard SDK supports ppt, pptx, doc, docx, and pdf files. Use the file upload control in the left sidebar to manage your file list.
File list persistence
The whiteboard SDK stores file lists in the browser's LocalStorage. Lists persist when you reopen the page but not across devices or browsers. To enable cross-device persistence, implement server-side interfaces that listen for whiteboard SDK events such as docAdd and docDelete. During initialization, call setDefaultDocList to restore the file list. For more information, see the Whiteboard SDK documentation.
Notes
The whiteboard SDK converts only the whiteboard canvas content into a video stream. Video or audio files added to the canvas cannot be shared through the whiteboard stream.
Publish the whiteboard stream
After the whiteboard SDK joins a channel, call the getStream interface to retrieve the video stream and publish it through the custom input feature.
// The drawPlugin object is returned after the whiteboard SDK instance successfully joins the channel.
const mediaStream = drawPlugin.getStream({
width: 720,
frameRate: 15,
});
const videoTrack = mediaStream.getVideoTracks()[0];
if (videoTrack) {
try {
await aliRtcEngine.startScreenShare({
videoTrack, // Pass the custom video track.
});
console.log('Whiteboard sharing started successfully.');
} catch (error) {
console.log('Failed to share the whiteboard.');
}
} else {
console.log('No whiteboard videoTrack available.');
}
Integration example
Prerequisites
This example extends the quick start example in the Quick Start document. Complete that example before proceeding.
Step 1: Create files
In the demo folder, create two new files: whiteboard.html and whiteboard.js. Place the three whiteboard SDK JS files, WhiteBoardSDK.js, ToolCollection.js, and pptRenderer.js, in the same folder. The directory structure is as follows:
- demo
- quick.html
- quick.js
- whiteboard.html
- whiteboard.js
- WhiteBoardSDK.js
- pptRenderer.js
- ToolCollection.js
Step 2: Edit whiteboard.html
Copy and paste the code below into whiteboard.html and save the file.
Step 3: Edit whiteboard.js
Copy and paste the code below into whiteboard.js, replace the placeholder aliyun-rtc-sdk Application ID and AppKey and the whiteboard SDK appKey and appsecret with your credentials, and save the file.
Step 4: Run the example
-
In your terminal, navigate to the demo folder and run http-server -p 8080 to start an HTTP service.
-
Open a new tab in your browser and navigate to localhost:8080/quick.html to act as a regular user. On the page, enter a Channel ID and a User ID, and then click Join Channel.
-
Open another new tab in your browser and navigate to localhost:8080/whiteboard.html to act as the whiteboard user. On the page, enter the same Channel ID as in the previous step and a different User ID, and then click Join Channel.
-
The page automatically subscribes to the other user's media stream. In whiteboard.html, click the Share Whiteboard button. The quick.html page then automatically subscribes to the whiteboard content from whiteboard.html.