MQTT (Message Queuing Telemetry Transport) is a message protocol based on the TCP/IP protocol stack and supports asynchronous communication between the parties. MQTT separates the sender from the receiver in space and time, so it can be expanded in an unreliable network environment. Although it is called Message Queuing Telemetry Transport, it does not involve message queuing. It uses the publication and subscription model.
The MQTT protocol of the Alibaba Cloud IoT Platform does not support "will" messages, and the CONNECT message content parameters are as follows:
clientId, username, and password are generated by the device trituples (productKey, deviceName, deviceSecret) in accordance with the following rules:
Example of device code (Node.js) client.js
/**
"dependencies": { "mqtt": "2.18.8" }
*/
const crypto = require('crypto');
const mqtt = require('mqtt');
// The trituples of the device identity + region
const deviceConfig = {
productKey: "replace",
deviceName: "replace",
deviceSecret: "replace",
regionId: "cn-shanghai"
};
//Generate mqtt connection parameters from the trituples
const options = initMqttOptions(deviceConfig);
const url = `tcp://${deviceConfig.productKey}.iot-as-mqtt.${deviceConfig.regionId}.aliyuncs.com:1883`;
//2. Establish the connection
const client = mqtt.connect(url, options);
client.on('packetsend', function (packet){
console.log('send '+packet.cmd+' packet =>',packet)
})
client.on('packetreceive', function (packet){
console.log('receive '+packet.cmd+' packet =>',packet)
})
//Initialization of mqtt connection parameters for IoT Platform
function initMqttOptions(deviceConfig) {
const params = {
productKey: deviceConfig.productKey,
deviceName: deviceConfig.deviceName,
timestamp: Date.now(),
clientId: Math.random().toString(36).substr(2),
}
// CONNECT parameter
const options = {
keepalive: 60, //60s
clean: false, //cleanSession maintains a persistent session
protocolVersion: 4 //MQTT v3.1.1
}
//1. Generate clientid, username, password
options.password = signHmacSha1(params, deviceConfig.deviceSecret);
options.clientId = `${params.clientId}|securemode=3,signmethod=hmacsha1,timestamp=${params.timestamp}|`;
options.username = `${params.deviceName}&${params.productKey}`;
return options;
}
/*
Generate a password based on HmacSha1
Reference documents: https://help.aliyun.com/document_detail/73742.html?#h2-url-1
*/
function signHmacSha1(params, deviceSecret) {
let keys = Object.keys(params).sort();
// Sort by lexicographical order
keys = keys.sort();
const list = [];
keys.map((key) => {
list.push(`${key}${params[key]}`);
});
const contentStr = list.join('');
return crypto.createHmac('sha1', deviceSecret)
.update(contentStr)
.digest('hex');
}
For more information about MQTT protocol, you can go to Introduction to the MQTT Protocol and Alibaba Cloud's IoT Platform.
This topic describes how to establish MQTT connections over TCP by using two methods: direct connection and connection after HTTPS verification.
Note: When you configure MQTT CONNECT packets:
IoT Platform supports MQTT over WebSocket. You can first use the WebSocket protocol to establish a connection, and then use the MQTT protocol to communicate on the WebSocket channel.
Using WebSocket has the following advantages:
Mosquitto is a lightweight, open source and machine-to-machine messaging protocol for communication between "Internet of Things" devices such as ESP8266, Raspberry Pi, etc. It is designed for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium. It is written in C language and suitable for use on all devices from low power single board computers to full servers. It is also ideal for mobile applications because of its small size, low power usage, minimized data packets, and efficient distribution of information to one or many receivers. Mosquitto is one of the most popular MQTT brokers due to its good community support, documentation and ease of installation.
There are very many message queues out there, from RocketMQ, RabbitMQ, Apache Kafka, ZeroMQ, MosquitoMQ, and many more. Many Cloud providers also offer managed message queues as a service and Alibaba Cloud has two of them namely the Message Queue and the Message Service.
With those many years of development, the Message Queue had more community experience and expertise behind it, but it also comes with more legacy methods and restrictions. For example, the Message Queue can only speak over TCP or MQTT protocols. This means you'll most likely require specific extensions enabled on your server to access these protocols. It also means you will most likely require an officially supported SDK to use it. Currently, Alibaba Cloud supports Python, PHP, .NET and Java programming languages. This means that both your producers and consumers must be written in one of those languages if you want official support.
The newer Message Service product differs from the Message Queue in that it can speak HTTP. This is a crucial differentiator implying that, so long as you can send a HTTP request, your application can use the Message Service. This opens the door to almost each and every programming language out there. There are official SDKs of course, but these are mostly to add synthetic sugar to make using it easier. Another decisive differentiator is that the Message Service has the ability to push out message to consumers instead of waiting for them to pull the messages out of the queue. The consumer can be any HTTP server, a browser via websockets, a mobile phone, an email address or even another Message Queue service!
Alibaba Cloud IoT Platform provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform and supports device access worldwide, from a variety of networks and providers that are all based on different protocols.
AlibabaMQ for Apache RocketMQ is a distributed message queue service that supports reliable message-based asynchronous communication among microservices, distributed systems, and serverless applications.
Alibaba Cloud's Next-Generation Security Makes Gartner's Report
2,599 posts | 762 followers
FollowAlibaba Clouder - September 24, 2019
Alex - July 9, 2020
Clouders - January 10, 2022
Alibaba Cloud New Products - June 2, 2020
GXIC - February 20, 2020
Alibaba Clouder - August 5, 2019
2,599 posts | 762 followers
FollowA message service designed for IoT and mobile Internet (MI).
Learn MoreA cloud solution for smart technology providers to quickly build stable, cost-efficient, and reliable ubiquitous platforms
Learn MoreProvides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.
Learn MoreMore Posts by Alibaba Clouder