All Products
Search
Document Center

ApsaraMQ for MQTT:User properties

Last Updated:Mar 11, 2026

User properties is a mechanism defined by MQTT 5.0 that allows you to add custom metadata in messages. Each user property is a UTF-8-encoded string pair that travels with a message, similar to HTTP headers, without altering the payload. This increases the flexibility and adaptability of message transmission and allows MQTT to efficiently process complex business logic.

Each user property consists of a key-value pair. Keys are case-sensitive and do not need to be unique -- the same key can appear multiple times. ApsaraMQ for MQTT preserves all entries without deduplication.

Use cases

  • Device status reporting: Attach device type and location to status messages so downstream consumers can filter or route data without parsing the payload.

  • Message routing: Include routing hints such as priority level or target region, enabling distributed systems to direct messages to the correct processing pipeline.

  • Audit and traceability: Embed session IDs, user identifiers, or correlation tokens to support security audits and end-to-end message tracing.

How it works

  1. A publisher attaches one or more user properties to a PUBLISH packet and sends it to a topic.

  2. ApsaraMQ for MQTT stores the user properties alongside the message and forwards them to all matching subscribers.

  3. Subscribers receive the full set of user properties with each delivered message, enabling context-aware processing without parsing the payload.

Quick start (Node.js)

The following example publishes a message with user properties and logs the received properties on the subscriber side using the mqtt.js library.

const mqtt = require('mqtt')

const client = mqtt.connect(process.env.MQTT_ENDPOINT, {
  clientId: process.env.MQTT_CLIENT_ID,
  username: process.env.MQTT_ACCESS_KEY,
  password: process.env.MQTT_SECRET_KEY,
  protocolVersion: 5  // MQTT 5.0 required for user properties
})

client.on('connect', () => {
  // Subscribe to the topic
  client.subscribe('sensors/temperature', { qos: 1 })

  // Publish a message with user properties
  client.publish('sensors/temperature', '22.4', {
    qos: 1,
    properties: {
      userProperties: {
        location: 'warehouse-section-a',
        timestamp: '2021-09-15T12:00:00Z'
      }
    }
  })
})

// Log incoming messages and their user properties
client.on('message', (topic, payload, packet) => {
  console.log('Topic:', topic)
  console.log('Payload:', payload.toString())
  console.log('User Properties:', packet.properties.userProperties)
})

Set the following environment variables before running the example:

Environment variableDescriptionExample
MQTT_ENDPOINTApsaraMQ for MQTT broker endpointmqtt://mqtt-cn-xxxxx.mqtt.aliyuncs.com
MQTT_CLIENT_IDMQTT client IDGID_test@@@device_01
MQTT_ACCESS_KEYAlibaba Cloud AccessKey IDLTAI5tXxx
MQTT_SECRET_KEYAlibaba Cloud AccessKey SecretxXxXxXx

Limits

ConstraintValue
Maximum size per user propertyA user property cannot exceed 8 KB in length after serialization
Supported packet typeOnly user properties in PUBLISH packets are stored and forwarded
EncodingKeys and values must be UTF-8-encoded strings
Key uniquenessNot required. Duplicate keys are preserved without deduplication

Serialization format example:

[{"key":"mQ","value":"test001"},{"key":"MQ","value":"test002"}]