Message Queuing Telemetry Transport (MQTT) is an asynchronous communication protocol based on the TCP/IP protocol stack. MQTT is a lightweight protocol that is used to transmit messages in the publish/subscribe model. MQTT is scalable in unreliable network environments. MQTT is suitable for scenarios in which the storage space of device hardware or network bandwidth is limited. The sender and receiver of a message that is transmitted over MQTT are not restricted by time or space. You can connect a device to IoT Platform over MQTT.
Differences between IoT Platform-based MQTT and standard MQTT
- Supports MQTT messages, such as PUB, SUB, PING, PONG, CONNECT, DISCONNECT, and UNSUB.
- Supports the clean session flag.
- Does not support will and retained messages.
- Supports quality of service (QoS) 0 and QoS 1 messages and does not support QoS 2 messages.
- Does not support QoS settings on subscriber clients. Only the QoS that is specified by publisher clients is valid.
- Supports the synchronous RRPC mode based on native MQTT topics. A server can call a device service and obtain a response at the same time.
Supported MQTT 5.0 features
Compared with the previous version, MQTT 5.0 provides a large number of new features to improve performance and user experience. For more information, see Appendix C. Summary of new features in MQTT 5.0.
IoT Platform supports the following new features of MQTT 5.0:
- Specify the maximum length of messages on clients and servers to filter messages.
MqttConnectionOptions connOpts = new MqttConnectionOptions(); connOpts.setMaximumPacketSize(1024L);
- Specify the maximum number of QoS 1 messages per second.
MqttConnectionOptions connOpts = new MqttConnectionOptions(); connOpts.setReceiveMaximum(5);
- Use the UserProperty parameter to specify a list of properties. This parameter is used to transmit additional
property data. Each property consists of a key and a value.
Note You can add up to 20 properties. Each key cannot start with an underscore (_). The total length of a key and a value cannot exceed 128 characters.
MqttProperties properties = new MqttProperties(); List<UserProperty> userPropertys = new ArrayList<>(); userPropertys.add(new UserProperty("key1","value1")); properties.setUserProperties(userPropertys);
After a device is connected to IoT Platform over MQTT 5.0, you can view the submitted UserProperty parameter in IoT Platform logs.
- Add the ResponseTopic and CorrelationData parameters to enable bidirectional communication
in a mode that is similar to HTTP-based request/response.
For example, the requester is a device, and the responder is your business server. After you use the Advanced Message Queuing Protocol (AMQP) subscription or data forwarding feature, you can parse the ResponseTopic and CorrelationData parameters from the property data in the message. Then, you can call the Pub operation to send a response to the device.
MqttProperties properties = new MqttProperties(); properties.setCorrelationData("requestId12345".getBytes()); properties.setResponseTopic("/" + productKey + "/" + deviceName + "/user/get");Note
- The parsed value of the CorrelationData parameter must be Base64 decoded to the byte array that is submitted by the device.
- The value of the ResponseTopic or CorrelationData parameter cannot exceed 128 characters in length.
- Add more response codes that can be used by devices to identify request statuses and
For more information, see Troubleshooting.
- Scale down message communication topics to integers to reduce the size of MQTT messages. This saves bandwidth resources.
- Transport Layer Security (TLS)-based TCP connection: high security.
- TLS 1.0, 1.1, and 1.2 are supported. We recommend that you use TLS 1.2 for encryption. TLS 1.0 and 1.1 are previous versions and may cause security risks.
- Device-side Link SDK is configured with TLS 1.2. If you use Link SDK, you do not need to configure the TLS protocol.
- IoT Internet Device ID-based TCP connection (IoT Internet Device ID is a chip-level encryption service): high security.
- Unencrypted TCP connection: low security. The feature will be phased out soon. We recommend that you do not use the feature.
For more information about the definitions and types of topics, see What is a topic?.
You can view system topics on the Device Details page of the IoT Platform console. For information about feature-specific topics, see the documentation about specific features.
After you register a device, you can use only one protocol to connect the device to IoT Platform. You cannot use multiple protocols for the same device.
IoT Platform provides various device SDKs. You can use the device SDKs to connect devices to IoT Platform over MQTT. For more information about how to obtain a device SDK, see Download device SDKs.
For more information about how to enable MQTT communication between devices and IoT Platform by using various communication protocols, see the following topics: