MQTT v5 brings many new features. We will try our best to display these features in an easy-to-understand way and discuss the impact of these features on developers. So far, we have discussed these new features of , today we will continue to discuss: user attributes .

What are user attributes

User Properties are actually a kind of custom properties that allow users to add their own metadata to MQTT messages and transmit additional custom information to expand more application scenarios.

It consists of a user-defined UTF-8 key/value pair array and is configured in the message attribute field. As long as the maximum message size is not exceeded, an unlimited number of user attributes can be used to add metadata to MQTT messages, and Pass information among publishers, MQTT servers and subscribers.

If you are familiar with the HTTP protocol, this function is very similar to the concept of HTTP Header. User attributes effectively allow users to extend the MQTT protocol and can appear in all messages and responses. Because user attributes are defined by the user, they are only meaningful to the user's realization.

Why you need to use user attributes

The protocol scalability of MQTT 3 is poor. The user attribute is actually to solve this problem. It supports the transmission of any information in the message and ensures that the user can extend the function of the standard protocol.

For selecting and configuring different message types, user attributes can be sent between the client and the MQTT server, or between the client and the client. When configuring user attributes in the connection client, it can only be received on the MQTT server, not on the client. If you configure user attributes when sending a message, you can receive it in other clients. The following two user attribute configurations are commonly used.

User attributes of the connected client

When the client initiates a connection with the MQTT server, the server can predefine some metadata information that needs and can be used, that is, user attributes. When the connection is successful, the MQTT service can use the related information sent by the connection, so The user attribute of the connected client depends on the MQTT server.

User attributes for message publishing

User attributes when publishing messages may be more commonly used because they can transfer metadata information between the client and the client. For example, you can add some common information when publishing: message number, timestamp, file, client information and routing information and other attributes.

In addition to the above-mentioned more commonly used user attribute settings, you can also configure user attributes when subscribing to a topic, when unsubscribing, and when disconnecting.

Use of user attributes

file transfer

The user attributes of MQTT 5 can be extended to use it for file transfer, instead of putting data in the payload of the message body as in the previous MQTT 3, the user attributes use key-value pairs. This also means that the file can be kept as binary because the metadata of the file is in the user properties. E.g:

{
  "filename": "test.txt",
  "content": "xxxx"
}

Resource analysis

After the client connects to the MQTT server, different clients, vendor platforms or systems have different ways to transmit message data, and there may be some structural differences in the format of message data. Some clients are distributed in different regions. For example, the message format sent by the device in region A is JSON, and the message format sent by the device in region B is XML. At this time, after the server receives the message, it may need to judge and compare one by one to find a suitable parser for data analysis.

At this time, in order to improve efficiency and reduce computational load, we can use the user attribute function to add data format information and geographic information. When the server receives the message, it can use the metadata provided in the user attribute to perform data analysis operations. And when the client of area A subscribes to receive the client message from area B, it can also quickly know which area the specific message comes from, so that the message is traceable.

{
  "region": "A",
  "type": "JSON"
}

MQTT 资源解析

Message routing

We can also use user attributes to do application-level routing. As mentioned above, there are different systems and platforms, and there are different devices in each area. Multiple systems may receive messages from the same device. Some systems need to display data in real time, and another system may display the data in real time. Perform timing storage. Therefore, the MQTT server can determine whether to distribute the message to the system that stores the message or the system that displays the data by reporting the user attributes configured in the message.

{
  "type": "real-time",
  "timestamp": 1636620444
}

MQTT 消息路由

Configure user properties in the client

Let's take the JavaScript environment as an example, and use the MQTT.js client for programming.

Note: When connecting to the client, you need to specify the MQTT version protocolVersion as 5.

connect

We set the User Properties property of properties in the options when connecting, and add the type and region properties. After the connection is successful, the MQTT server will receive this user-defined message.

// connect options
const OPTIONS = {
  clientId: 'mqtt_test',
  clean: true,
  connectTimeout: 4000,
  username: 'emqx',
  password: 'public',
  reconnectPeriod: 1000,
  protocolVersion: 5,
  properties: {
    userProperties: {
      region: 'A',
      type: 'JSON',
    },
  },
}
const client = mqtt.connect('mqtt://broker.emqx.io', OPTIONS)

make an announcement

After the connection is successful, the message is published, the user attribute is set in the configuration of the message, and the message reception is monitored. In the publish function, we configure user properties and print the packet in the function that receives the message.

client.publish(topic, 'nodejs mqtt test', {
  qos: 0,
  retain: false,
  properties: {
    userProperties: {
      region: 'A',
      type: 'JSON',
    },
  },
}, (error) => {
  if (error) {
    console.error(error)
  }
})
client.on('message', (topic, payload, packet) => {
  console.log('packet:', packet)
  console.log('Received Message:', topic, payload.toString())
})

At this point, we can see that the user attributes configured just before sending have been printed and output in the console.

MQTT 消息接收

For other clients, we will cross-platform MQTT 5.0 desktop client tool-MQTT X , so that users can quickly test some new features of MQTT 5.0, so stay tuned!


EMQX
336 声望436 粉丝

EMQ(杭州映云科技有限公司)是一家开源物联网数据基础设施软件供应商,交付全球领先的开源 MQTT 消息服务器和流处理数据库,提供基于云原生+边缘计算技术的一站式解决方案,实现企业云边端实时数据连接、移动、...