ApsaraMQ for MQTT supports the payload format indicator feature of Message Queuing Telemetry Transport (MQTT) 5.0. This topic describes the payload format indicator feature.
Feature description
Payload format indicator is a unique feature in MQTT 5.0. This feature is used to indicate whether the payload of a PUBLISH message is in the predefined format.
The payload format indicator feature makes it easier for the receiver to interpret the payload of a PUBLISH message. You can set the Payload Format Indicator parameter to 0 or 1. The value 0 indicates that no format is specified for the payload. The value 1 indicates that the payload is a string encoded in UTF-8.
If you set the Payload Format Indicator parameter to 1 when you send a message from a client to a broker, the broker verifies whether the message is in the UTF-8 format. If the message is not in the UTF-8 format, the message fails to be sent. If the QoS level of the message is 1, the error message PAYLOAD_FORMAT_INVALID is returned in the PubAck packet.
Scenarios
Format negotiation: This feature allows publishers and subscribers to reach a consensus on the format of message content. For example, if you set the Payload Format Indicator parameter to 1, which indicates that the payload of the message is encoded in UTF-8, this tells the subscriber how to decode and process the payload when the subscriber receives the message.
Decoding verification: If you specify the payload format indicator for a message, the receiver can verify whether the message is encoded in the expected format. This helps the receiver process message content in a more secure manner.
Interoperability and compatibility: When multiple clients or services share an MQTT system, you can use the payload format indicator feature to ensure that all clients can interpret and understand the message content.
Example
An MQTT client publishes a message in JSON format from a topic. The client wants other clients that subscribe to the topic to know that the payload of the message is a JSON text encoded in UTF-8.
PUBLISH
Topic: sensor1/data
Payload Format Indicator: 1 // The payload of the message is encoded in the predefined format UTF-8.
Payload: {"temperature": 22.3, "humidity": 48}In the preceding example, the Payload Format Indicator parameter is set to 1. This way, the publisher tells the subscriber that the payload of the PUBLISH message is a JSON string encoded in UTF-8 instead of binary data or data encoded in another format. In this case, all clients that subscribe to the sensor1/data topic recognize the payload format and can correctly parse the JSON data.
The payload format indicator feature improves the accuracy and efficiency of data transmission in MQTT 5.0 and ensures that message data from one client can be interpreted and correctly processed by other clients. If multiple systems with different data formats are integrated, the payload format indicator feature can help greatly increase the processing speed.