How does IoT platform realize 1 million/s message broadcasting?

In the IoT scenario, there is often a demand for full device message broadcasting. How to instantly push messages to mass online devices?

Alibaba Cloud IoT enterprise IoT instance provides developers with a PubBroadcast interface, which can reach 1 million online devices at the second level, so that enterprises have no worries when facing high concurrency requirements.

Create 4 devices and broadcast content

Let's take the collection speaker scenario message broadcast as an example to demonstrate the full volume equipment broadcast function.

Equipment and broadcast message body

Full volume device broadcast interface PubBroadcast

Example of message received by the device:

Topic example:
/sys/g6palBDnzyT/dk004/broadcast/request/1386995865331441413

Payload example:
{
"volume": 80,
"mode": 4
}

Full broadcast development practice

Device side development

The IoT Internet of Things platform provides full broadcast topics by default, and the device side can receive broadcast messages from the cloud without pre subscription.

Device side application code (Node.js example):
// node broadcast-device.js
const mqtt = require('aliyun-iot-mqtt');
//1. Device identity triplet+region
const options = require("./iot-device-config.json");
//2. Establish MQTT connection
const client = mqtt.getAliyunIotMqttClient(options);
//System full broadcast Topic prefix
const allBroadcastTopic = `/sys/${options.productKey}/${options.deviceName}/broadcast/request`;
client. on('message', function(topic, message) {
//Filter full broadcast messages
if (topic.indexOf(allBroadcastTopic) > -1) {
Console. log ("The device received a full broadcast message:")
console. log(" topic=" + topic)
console. log(" message=" + message)
}
})

iot-device-config. Json device configuration parameters:

{
productKey: "g6palBDnzyT",
deviceName: "dk004",
deviceSecret: "424ed56661980c604255507d1b81464e",
host: "iot-060a08kg.mqtt.iothub.aliyuncs.com"
}

Cloud back-end development

The cloud business system calls the PubBroadcast interface of the IoT IoT instance to send broadcast messages and reach a full range of online devices. Call code example (Node. js):

// node broadcast.js
const co = require('co');
const RPCClient = require('@alicloud/pop-core').RPCClient;
const options = {
AccessKey: "Your own accessKey",
AccessKeySecret: "Your own accessKeySecret"
};

//1. Create a client

const client = new RPCClient({

accessKeyId: options.accessKey,

secretAccessKey: options.accessKeySecret,

endpoint: ' https://iot.cn-shanghai.aliyuncs.com ',

apiVersion: '2018-01-20'

});

//2. Construct iot API

//Here is the Action of POP API

const action = 'PubBroadcast';

//Broadcast content

const payload = {

volume: 80,

mode: 4

};

//Complete parameter params

const params = {

ProductKey: "g6palBDnzyT",

IotInstanceId: "iot-064a04kg",

MessageContent: new Buffer(JSON.stringify(payload)).toString("base64")

};

co(function*() {

//3. Launch full broadcast PubBroadcast API call

try {

const response = yield client. request('PubBroadcast', params);

console. Log ("PubBroadcast Success=========>", JSON. stringify (response));

} catch (err) {

console. Log ("PubBroadcast failed========>", JSON. stringify (err));

}

});

Full broadcast joint debugging

Log of business server calling PubBroadcast API:

Log of broadcast messages received by four device terminals:

Topic has the same message id, but different deviceName.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us