This topic describes how to send and receive custom messages and provides sample code.
Sample code
Send and receive custom messages on Android: Android/ARTCExample/BasicUsage/src/main/java/com/aliyun/artc/api/basicusage/DataChannelMessage/DataChannelMessageActivity.java.
Send and receive custom messages on iOS: iOS/ARTCExample/BasicUsage/DataChannelMessage/DataChannelMessageVC.swift.
Prerequisites
You have a valid Alibaba Cloud account and have created an ApsaraVideo Real-time Communication application. For more information, see Create an application. Obtain an App ID and an App Key in the ApsaraVideo Real-time Communication console.
You have integrated the ARTC SDK into your project and implemented basic real-time audio and video features. For information about SDK integration, see Download and integrate the SDK. For information about implementing audio and video calls, see Implement an audio and video call.
Implementation

A streamer can send and receive messages. A viewer can only receive messages.
Enable the custom message channel
The custom message channel is disabled in ARTC by default. To enable it, you can call the setParameter method. You can call this method before or after joining a channel.
Android
param = "{\"data\":{\"enablePubDataChannel\":true" + ",\"enableSubDataChannel\":true}}";
mAliRtcEngine.setParameter(param);iOS
NSString* parameter = [NSString stringWithFormat:@"{\"data\":{\"enablePubDataChannel\":true,\"enableSubDataChannel\":true}}"];
[self.engine setParameter:parameter];Web
// On the web, you must call this method before joinChannel.
mAliRtcEngine.setParameter(
JSON.stringify({
data: {
enablePubDataChannel: true,
enableSubDataChannel: true,
},
}),
);Windows
char * param = "{\"data\":{\"enablePubDataChannel\":true,\"enableSubDataChannel\":true}}";
mAliRtcEngine->SetParameter(param);Send a custom message
After the data channel is established, you can call the sendDataChannelMsg method.
A streamer can send and receive messages. A viewer can only receive messages.
You must call
setParameterto enable the custom message channel.The following limits apply to sending data:
The maximum bitrate is 30 KB/s.
The data channel can send a maximum of 60 packets per second. Each packet can be up to 1 KB in size.
Android
AliRtcEngine.AliRtcDataChannelMsg msg = new AliRtcEngine.AliRtcDataChannelMsg();
msg.type = AliRtcEngine.AliRtcDataMsgType.AliEngineDataMsgCustom;
/* data can be any data, such as text or images. */
msg.data = xxxx;
mAliRtcEngine.sendDataChannelMsg(msg);iOS
AliRtcDataChannelMsg* msg = [[AliRtcDataChannelMsg alloc] init];
msg.type = AliRtcDataMsgCustom;
/* data can be any data, such as text or images. */
msg.data = xxxxx;
[self.engine sendDataChannelMessage:msg];Web
const data = new TextEncoder().encode('xxxx');
// data can be any data, such as text or images. The format is ArrayBuffer.
mAliRtcEngine.sendDataChannelMessage(
new AliRtcDataChannelMsg(data),
);Windows
AliEngineDataChannelMsg msg;
msg.type = AliEngineDataChannelCustom;
/* data can be any data, such as text or images. */
msg.data = data;
msg.dataLen = dataLength;
mAliRtcEngine->SendDataChannelMessage(msg);Receive a custom message
You can listen for the onDataChannelMessage callback to receive custom messages.
Android
// In the AliRtcEngineNotify callback
public void onDataChannelMessage(String uid, AliRtcEngine.AliRtcDataChannelMsg msg) {
/* TODO: Add your custom message processing logic. */
}iOS
- (void)onDataChannelMessage:(NSString *_Nonnull)uid controlMsg:(AliRtcDataChannelMsg*_Nonnull)controlMsg {
/* TODO: Add your custom message processing logic. */
}Web
mAliRtcEngine.on('dataChannelMsg', (uid, message) => {
// Add your custom message processing logic.
console.log('dataChannelMsg', uid, message);
});Windows
public:
virtual void OnDataChannelMessage(const char* uid, const AliEngineDataChannelMsg& msg) override {
/* TODO: Add your custom message processing logic. */
}