Portal: 5 video explanations, 30 scene case summary
In IoT scenarios, there are often requests for broadcasting messages from all devices. How to instantly push messages to massive online devices?
The Alibaba Cloud IoT Enterprise IoT instance provides developers with a PubBroadcast interface, which can reach 1 million online devices in seconds, so that enterprises can have no worries when facing high concurrent demands.
Create 4 devices and broadcast content
Let's take the scene message broadcasting of the cashier speaker as an example to demonstrate the broadcasting function of all devices.
Device and Broadcast Message Body
Full device broadcast interface PubBroadcast
Example of the message message received by the device: Topic example:
Topic Example: /sys/g6palBDnzyT/dk004/broadcast/request/1386995865331441413 Payload Example: { "volume": 80, "mode": 4 }
Full broadcast development practice
Device-side development
The IoT platform provides a full amount of broadcast topics by default, and devices can receive broadcast messages from the cloud without pre-subscribing. 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 an MQTT connection const client = mqtt.getAliyunIotMqttClient(options); //The system fully broadcasts the Topic prefix const allBroadcastTopic = `/sys/${options.productKey}/${options.deviceName}/broadcast/request`; client.on('message', function(topic, message) { //Filter all broadcast messages if (topic.indexOf(allBroadcastTopic) > -1) { console.log("The device receives all broadcast messages:") console.log("\ttopic=" + topic) console.log("\tmessage=" + message) } })
iot-device-config.json device configuration parameters:
{ productKey: "g6palBDnzyT", deviceName: "dk004", deviceSecret: "424ed56661980c604255507d1b81464e", host: "iot-060a08kg.mqtt.iothub.aliyuncs.com" }
Cloud background development
The cloud business system calls the PubBroadcast interface of the IoT instance to send broadcast messages and reach all online devices. Call code example (Node.js):
// node broadcast.js const co = require('co'); const RPCClient = require('@alicloud/pop-core').RPCClient; const options = { accessKey: "my own accessKey", accessKeySecret: "my 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 the iot API // Here is the Action of the POP API const action = 'PubBroadcast'; // broadcast content const payload = { volume: 80, mode: 4 }; // Complete input params const params = { ProductKey: "g6palBDnzyT", IotInstanceId: "iot-064a04kg", MessageContent: new Buffer(JSON.stringify(payload)).toString("base64") }; co(function*() { //3. Initiate 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 fail =====>", JSON.stringify(err)); } });
Full broadcast joint debugging
The business server calls the PubBroadcast API log:
Broadcast message logs received by 4 devices:
Topic message id is the same, deviceName is different
Past recommendation
1. Summary of 39 sensor working principle GIF s
2. Development practice of intelligent handheld temperature measuring gun
3. JMeter pressure test MQTT service performance actual combat
4. Detailed Explanation of IoT Platform Log Service
5. Practice of self-built MQTT cluster migration to Alibaba Cloud IoT
6. Industrial Modbus power 104 protocol access to IoT platform
7. The device does not need to burn triplets, real-time registration solution
8. IoT+TSDB+Quick BI builds building environment monitoring
9. JS full-stack development, building smart home applets
IoT platform product introduction details: https://www.aliyun.com/produc...
Alibaba Cloud IoT Platform Customer Exchange Group