All Products
Search
Document Center

ApsaraMQ for MQTT:User properties

Last Updated:Aug 22, 2024

This topic describes the user properties feature of Message Queuing Telemetry Transport (MQTT) 5.0.

Feature description

The user properties feature is a mechanism that is defined by MQTT 5.0 to allow you to add custom metadata in messages. User properties are displayed as UTF-8 strings in the key-value format. User properties are similar to HTTP headers and do not affect the content of messages. You can use user properties to configure custom metadata based on your business scenarios. This increases the flexibility and adaptability of message transmission and allows MQTT to efficiently process complex business logic.

Limits

  • A user property cannot exceed 8 KB in length after serialization.

    The following sample code provides an example of serialized user properties:

    [{"key":"mQ","value":"test001"},{"key":"MQ","value":"test002"}]
  • Only user properties in published messages are stored and forwarded.

Usage notes

  • User properties are displayed as key-value pairs. The keys of user properties can be the same. MQTT does not perform deduplication on keys.

  • Each user property consists of a key-value pair. The key and value of a user property must be strings encoded in UTF-8.

Scenarios

  • Device status reporting: You can use user properties to add information, such as device type and location, to the status data of a device. This allows the data recipient to perform specific operations during data processing based on the user properties.

  • Message routing: You can route messages in distributed systems based on the values of user properties. This improves the flexibility of message processing.

  • Security and audit: When you transmit sensitive data, you can use user properties to specify audit information, such as the session ID and user identifier. This facilitates subsequent security audit and tracing.

The user properties feature of MQTT 5.0 allows data recipients to obtain additional context information about messages. This improves data processing efficiency and helps meet the requirements of various scenarios.

Example

You want to send a temperature-reading message that includes the location of the device and the timestamp that indicates when the temperature is read. In this case, you can create the following user properties:

Key: location. Value: warehouse-section-a.

Key: timestamp. Value: 2021-09-15T12:00:00Z.

When you use an MQTT client to construct the message, you can add the preceding user properties to the message together with the topic and payload. The following sample code provides an example of the PUBLISH packet that is sent to the MQTT broker:

Topic: sensors/temperature
Payload: 22.4 // the temperature that is read.
User Properties: 
  "location : warehouse-section-a"
  "timestamp: 2021-09-15T12:00:00Z"

After the MQTT client receives the message, in addition to the temperature, the location and time are also included in the data to improve the data processing efficiency.