Unsubscribing from topics with a GUI tool

Whenever we don't want a subscriber to receive more messages whose destination topic name matches one or more topic filters, the subscriber can send a request to unsubscribe to a list of topic filters to the MQTT server. Obviously, unsubscribing from topic filters is the opposite of subscribing to topic filters. We will use the MQTT.fx GUI utility to unsubscribe the MQTT client from the sensors/octocopter01/altitude topic. Follow these steps:

  1. Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
  2. Click Subscribe.
  1. Click on the panel that displays the sensors/octocopter01/altitude topic name on the left-hand side of the window. Then, click on the Unsubscribe button located in this panel. The following screenshot shows this button:
  1. MQTT.fx will unsubscribe the client from the sensors/octocopter01/altitude topic, and therefore the client won't receive any new messages published to the sensors/octocopter01/altitude topic.

Now, we will use the MQTT.fx GUI utility to make the MQTT client publish another message to the sensors/octocopter01/altitude. Follow these steps:

  1. Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
  2. Click Publish and enter sensors/octocopter01/altitude in the dropdown at the left-hand side of the Publish button.
  3. Then, click the Publish button. MQTT.fx will publish the entered text to the specified topic.
  1. Enter the following text in the textbox below the Publish button: 37 f, as shown in the following screenshot:

If you don't want to work with the MQTT.fx utility, you can run a mosquitto_pub command to generate another MQTT client that publishes a message to the topic. 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:

mosquitto_pub -V mqttv311 -t sensors/octocopter01/altitude -m "37 f"

Now, go back to the MQTT.fx window and click Subscribe to check the messages that have been received. The client has unsubscribed from the sensors/octocopter01/altitude topic before we published a new message to this topic, and therefore the recently published message with a payload of  "37 f" isn't displayed.

Go back to the Terminal or Command Prompt window in which you executed the mosquitto_sub command and subscribed to the sensors/octocopter01/atitude topic. You will see lines similar to the following:

Client mosqsub|3476-LAPTOP-5DO received PUBLISH (d0, q0, r0, m0, 'sensors/octocopter01/altitude', ... (4 bytes))
37 f

This client is still subscribed to the sensors/octocopter01/altitude topic, and therefore it received the message with the payload of "37 f".

The MQTT client sends an UNSUBSCRIBE packet to the MQTT server with a packet identifier (PacketId) in the header and one or more topic filters in the payload. The main difference with a SUBSCRIBE packet is that it isn't necessary to include the QoS level for each topic filter because the MQTT client just wants to unsubscribe.

After an MQTT client unsubscribes from one or more topic filters, the MQTT server still keeps the connection open; the subscriptions to topic filters that don't match the topic filters specified in the UNSUBSCRIBE packet payload will continue working.

Hence, a single UNSUBSCRIBE packet can ask the MQTT server to unsubscribe a client from many topics. The UNSUBSCRIBE packet must include at least one topic filter in the payload to comply with the protocol.

In the previous example, in which we asked the MQTT server to unsubscribe, we used a specific topic name as the value for the topic filter, and therefore we requested the MQTT server to unsubscribe from a single topic. As previously mentioned, we will learn about the use of wildcards in topic filters later.

The packet identifier will have a number value to identify the packet and make it possible to identify the response related to this UNSUBSCRIBE packet. The MQTT server will process a valid UNSUBSCRIBE packet and it will respond with an UNSUBACK  packet, which indicates the unsubscribe acknowledgment and confirms the receipt and processing of the UNSUBSCRIBE packet. The UNSUBACK packet will include the same packet identifier (PacketId) in the header that was received in the UNSUBSCRIBE packet.

The MQTT server will remove any topic filter that exactly matches any of the specified topic filters in the UNSUBSCRIBE packet's payload for the specific client that sent the packet. The topic filter match must be exact to be deleted. After the MQTT server deletes a topic filter from the subscription list for the client, the server stops adding new messages to be published to the client. Only messages that have already started delivery to the client with QoS levels of 1 or 2 will be published to the client. In addition, the server might publish existing messages that have been buffered for their distribution to the subscriber.

The following diagram shows the interaction between an MQTT client and an MQTT server in unsubscribing from 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.117.227.194