Subscribing to topics with a GUI tool

MQTT.fx is a GUI utility implemented with JavaFX that is available for Windows, Linux, and macOS. This tool allows us to connect with an MQTT server, subscribe to topic filters, see received messages, and publish messages to topics. You can download the appropriate version for your operating system from the downloads section of the main web page for this utility: http://www.mqttfx.org.

Now, we will use the MQTT.fx GUI utility to generate another MQTT client that subscribes to the same topic, sensors/octocopter01/altitude, and displays all the messages it receives. We will work with MQTT.fx version 1.6.0. Follow these steps:

  1. Launch MQTT.fx, select local mosquitto in the dropdown located at the upper-left corner, and click on the configuration icon at the right-hand side of this dropdown and at the left-hand side of the Connect button. MQTT.fx will display the Edit Connection Profiles dialog box with different options for the connection profile named local mosquitto. We analyzed many of these options when we learned about the data that the MQTT client sends to the MQTT server to establish a connection.
  2. Make sure the General button is pressed and make sure the MQTT Version Use Default checkbox is deactivated. Make sure 3.1.1 is selected in the dropdown below MQTT Version. This way, we tell the MQTT server that we want to use MQTT version 3.11. Notice that the Client ID textbox specifies MQTT_FX_Client. This is the ClientId value that MQTT.fx will send to the MQTT server (Mosquitto) in the CONNECT control packet. The following screenshot shows a dialog box with the selected options:
  1. Click OK and then click on the Connect button. MQTT.fx will establish a connection with the local Mosquitto server. Notice that the Connect button is disabled and the Disconnect button is enabled because the client is connected to the MQTT server.
  1. Click Subscribe and enter sensors/octocopter01/altitude in the dropdown at the left-hand side of the Subscribe button. Then, click the Subscribe button. MQTT.fx will display a new panel at the left-hand side with the topic to which we have subscribed, as shown in the following screenshot:

If you don't want to work with the MQTT.fx utility, you can run another mosquitto_sub command to generate another MQTT client that subscribes to the topic and print all the messages it receives. You just need to open another Terminal in macOS or Linux, or another Command Prompt in Windows, go to the directory in which Mosquitto is installed, and run the following command again. In this case, it isn't necessary to specify the -d option as given here:

mosquitto_sub -V mqttv311 -t sensors/octocopter01/altitude

Now, we have two MQTT clients subscribed to the same topic: sensors/octocopter01/altitude. Now, we will understand what happens under the hood when a client subscribes to a topic.

The MQTT client sends a SUBSCRIBE packet to the MQTT server with a packet identifier (PacketId) in the header and one or more topic filters with their desired quality of service level in the payload.

Quality of service is known as QoS.

Hence, a single SUBSCRIBE packet can ask the MQTT server to subscribe a client to many topics. The SUBSCRIBE packet must include at least one topic filter and a QoS pair to comply with the protocol.

In the two cases in which we requested a subscription, we used a specific topic name as the value for the topic filter, and therefore we request the MQTT server to subscribe to a single topic. We will learn about the use of wildcards in topic filters later.

We used the default options, and therefore the quality of service requested is the default level of 0. We will dive deep into QoS levels later. Now, we will stay focused on the simplest subscription cases. If the QoS level is equal to 0, the value for the PacketId field will be 0. If the QoS level is equal to 1 or 2, the packet identifier will have a number value to identify the packet and make it possible to identify the responses related to this packet.

The MQTT server will process a valid SUBSCRIBE packet and it will respond with a SUBACK packet that indicates the subscribe acknowledgment and confirms the receipt and processing of the SUBSCRIBE packet. The SUBACK packet will include the same packet identifier (PacketId) in the header that was received in the SUBSCRIBE packet. The SUBACK packet will include one return code for each pair of a topic filter and the desired QoS level received in the SUBSCRIBE packet. The number of return codes will match the number of topic filters included in the SUBSCRIBE packet. The following table shows the possible values for these return codes. The first three return codes indicate a successful subscription and each value specifies the maximum QoS that can be delivered based on the requested QoS and the possibilities that the MQTT server has to grant the requested QoS:

ReturnCode value Description
0 Successfully subscribed with a maximum QoS of 0
1 Successfully subscribed with a maximum QoS of 1
2 Successfully subscribed with a maximum QoS of 2
128 Failed to subscribe

 

If the subscription was successful, the MQTT server will start sending every published message that matches the topic filters specified in the subscription to the MQTT client with the QoS specified in the return code.

The following diagram shows the interaction between an MQTT client and an MQTT server to subscribe to one or many topic filters:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.169.94