Recently, MQTT X, the cross-platform MQTT 5.0 desktop test client open sourced by EMQ, released the latest version v1.7.0. MQTT X supports the rapid creation of multiple simultaneous online MQTT clients, which is convenient for testing the connection, publishing, and subscription functions of MQTT/TCP, MQTT/TLS, MQTT/WebSocket, and other MQTT protocol features.

The newly released version 1.7.0 implements more comprehensive support for MQTT 5.0, and is the most complete desktop test client tool in the world that supports MQTT 5.0 so far. At the same time, many new functions have been added to optimize the user experience.

In this article, we will introduce the specific operation of the new functions of MQTT X v1.7.0 in detail, especially how to use MQTT X to test many features of MQTT 5.0, so that readers can better apply MQTT 5.0 in actual projects.

Prepare the MQTT message server

Before using MQTT X v1.7.0 to test the features of MQTT 5.0, we first need to prepare the MQTT Broker that supports MQTT 5.0.

This article will use the free online MQTT 5.0 server provided by EMQ X Cloud with the MQTT X client for testing. As a fully managed cloud-native MQTT 5.0 messaging service, EMQ X Cloud can quickly create an MQTT service within minutes and fully supports the MQTT 5.0 protocol. It is also the world's first fully managed MQTT 5.0 public cloud service.

Before the test starts, the server access information of the free online MQTT 5.0 server is as follows:

  • Broker: broker.emqx.io
  • TCP Port: 1883
  • SSL/TLS Port: 8883

MQTT 5.0 test

User properties

In version 1.7.0, we first supported the configuration of user properties. User attribute is a very useful feature in MQTT 5.0. It is a custom attribute that allows users to add their own metadata to MQTT messages and transmit additional custom information to expand more application scenarios, such as message distribution, File transfer, language distinction, etc. This function is very similar to the concept of HTTP Header. We can configure user properties when creating client connections and publishing messages.

client connection

Click the New button to come to the page of creating a new client. First, we need to select the MQTT version as 5.0, so that you can see the card for configuring user attributes appears below. In the card is an input box that can configure key-value pairs. You can click Click the Add button in the upper right corner to add user attribute configuration, click the delete button at the end of each row to delete the configuration, and finally enter the attribute name and content to be configured. After the connection is successful, the MQTT server can obtain the user attribute content of the client.

MQTT 连接时的用户属性

news release

In addition to the configuration of user attributes when the client connects, this version also supports the configuration of user attributes when publishing messages. When creating a new connection as a client of MQTT 5.0, we can see a Meta button appears in the area where the message is published in the lower right corner. Clicking this button will bring up a card for configuring the properties at the time of publishing. We can see the user at the top of the card. property configuration.

MQTT 发布时的用户属性

After configuring the user attributes, click the Save button. At this time, we enter the Topic and Payload and click Send. You can see that the sent message box contains the content of the user attributes contained in the current message. When user attributes are also included, we can also see the user attribute configuration sent by the client in the received message box.

消息框内的用户属性

MQTT X's support for user attributes can help developers quickly verify and test the function when testing and verifying application scenarios with the MQTT 5.0 user attribute function, thereby improving development and usage efficiency.

request response

In version 1.7.0, the request response in MQTT 5.0 is supported, providing configuration of the response topic and comparison data properties, and controlling the response message to be routed back to the requesting publisher.

Because the MQTT protocol is based on the Pub/Sub mode, which is different from the request-response mode like the HTTP protocol, it is difficult for us to receive some response messages. For example, when we want to test and issue a control command, it is difficult for us to obtain the response after the command is sent. Although it can be implemented, it is too complicated. The request topic in MQTT 5.0 can implement this capability faster and more efficiently.

Let's take an example of issuing a command to turn the light on and off and respond to the command status to show how to use the response theme. We click the Meta button, enter a response topic in the input box: /ack/1, enter a comparison data: light, and subscribe to a /ack/1 on the currently connected client.

Note: MQTT's request response is asynchronous, and comparison data can associate the response message with the request message.

MQTT 响应主题与对比数据

We use MQTT.js to implement another client to simulate a light device that receives control commands. When the light-on command is received, a response message of successful turn-on is sent to the response topic. Implement key code:

client.on('message', (topic, payload, packet) => {
  console.log('Received Message:', topic, payload.toString())
  if (packet.properties && packet.properties.responseTopic) {
    client.publish(packet.properties.responseTopic, 'Success!', {
      qos: 0,
      retain: false,
    })
  }
})

Click send message, we can receive the response message from the light device after receiving the switch command successfully.

MQTT 发布消息

However, currently MQTT X only supports the configuration of the response topic and comparison data when sending for the request-response feature. In the future, we will continue to optimize the configuration of the response part to bring users a more complete ability to test the request response.

Content Type and Payload Format

In version 1.7.0, there is support for specifying the format and content type of the configuration payload. Allows specifying payload format (binary, text) and MIME-style content-type when publishing a message. We only need to click the Meta button before publishing the message, enter the Content Type in the input box, click to set the value of the Payload Format Indicator, and then publish the message.

MQTT 内容类型和载荷格式

A typical application of content type is to store MIME types, such as text/plain for text files, audio/aac for audio files, and application/json for an application message in JSON format.

When the value of the payload indicator property is set to false, the message is undetermined bytes, and when the value of this property is set to true, it means that the payload in the message body is UTF-8 encoded character data.

This will help the MQTT client or MQTT server to parse the message content more efficiently, without having to deliberately judge the format or type of the message body.

Subscription options

The subscription option in MQTT 5.0 is also supported in version 1.7.0. After creating a new MQTT 5.0 connection, we open the pop-up box of the subscription topic, and we can see that the configuration options including No Local, Retain as Published and Retain Handling appear below. Users can use these subscription options to change the behavior of the server.

MQTT 订阅选项

Set the No Local flag to true, then the server will not forward your own messages to you. Otherwise, if you subscribe to a topic that you publish messages to, then you will receive all the messages you publish.

When the Retain as Published flag is set to true, you can specify whether the server should keep the Retain identifier when forwarding the message to the client, instead of the client directly relying on the Retain identifier in the message to distinguish whether this is a normal forwarded message or a reserved one. information.

Retain Handling This option is used to specify whether the server sends a retain message to the client when the subscription is established. Set to 0, as long as the client subscribes successfully, the server will send the reserved message; if set to 1, the client subscribes successfully and the subscription does not exist before, the server will only send the reserved message; if set to 2, even if the client subscribes successfully, the server Hold messages are also not sent.

In subsequent versions, we will continue to support new features in MQTT 5.0 such as subscription identifiers.

Other new features to use

One-click multi-topic subscription

In the previous version, we could only subscribe to one topic each time we opened the pop-up box for subscribing topics. For users who want to subscribe to multiple topics, they need to click on and off each time to subscribe to multiple topics, which is not very convenient. . Therefore, in this version, we have optimized it to support subscribing to multiple topics at one time.

After we open the pop-up box of the subscription topic, in the Topic input box, enter multiple topics and separate them with commas (,). After clicking to confirm the subscription is successful, we can see that the subscription list contains multiple topics. For the alias function using the client layer, it can also support setting multiple topics at the same time. Similarly, use commas to separate (,).

Note: The content in the alias input box needs to correspond one-to-one with the Topic in the Topic input box.

MQTT 一键多主题订阅

Disable automatic scrolling of messages

Added control over the automatic scrolling of the message list when receiving and publishing messages in the settings. You can turn on the automatic scrolling function in the settings page, which is suitable for helping users to view the latest news when the message reception rate is slow. When the rate of receiving messages is too fast, you can click to turn off this function to help users view some old messages that have been sent or received.

Note: Some performance improvements when sending and receiving messages can be improved when autoscrolling is turned off.

MQTT X 禁止消息自动滚动

Epilogue

Through this article, I believe that everyone has a better understanding of the function usage of MQTT X v1.7.0. The use of MQTT X in conjunction with EMQ X can help you fully grasp the MQTT 5.0 protocol and better apply its features in actual projects.

In the future, we will also improve MQTT X's support for configurations such as topic aliases, request responses, and subscription identifiers, so stay tuned.


EMQX
336 声望436 粉丝

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