Unsubscribing from topics
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 operation of subscribing to topic filters. We will use the MQTT.fx GUI utility to unsubscribe the MQTT client from the sensors/drone01/altitude
topic. Follow the next steps:
- Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
- Click Subscribe.
- Click on the panel that displays the
sensors/drone01/altitude
topic name at the left-hand side of the window. Then, click on the Unsubscribe button located in this panel. The following screenshot shows this button. - MQTT.fx will unsubscribe the client from the
sensors/drone01/altitude
topic, and therefore, the client won't receive any new message published to thesensors/drone01/altitude
topic.
Now, we will use the MQTT.fx GUI utility to use the MQTT client to publish another message to the sensors/drone01/altitude
 topic. Follow the next steps:
- Go to the MQTT.fx window in which you established a connection and subscribed to a topic.
- Click Publish and enter
sensors/drone01/altitude
in the dropdown at the left-hand side of the Publish button. - Then, click the Publish button. MQTT.fx will publish the entered text to the specified topic.
- Enter the following text in the textbox below the Publish button:
30 f
, as shown in the following screenshot:
In case 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/drone01/altitude -m "30 f"
Now, go back to the MQTT.fx window and click Subscribe. The client has unsubscribed from the sensors/drone01/altitude
topic before we published a new message to this topic, and therefore, this published message 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/drone01/atitude
topic. You will see lines similar to the following ones:
Client mosqsub/5532-Gastons-Ma received PUBLISH (d0, q0, r0, m0, 'sensors/drone01/altitude', ... (4 bytes)) 30 f
This client is still subscribed to the sensors/drone01/altitude
topic, and therefore, it received the message with the payload of "30 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.
Tip
After an MQTT client unsubscribes from one or more topic filters, the MQTTserver still keeps the connection opened and the subscriptions to the topic filters that don't match the topic filters specified in the UNSUBSCRIBE packet payload will continue working.
Hence, a single UNSUBSCRIBE
packet can request 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 requested 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 usage 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 that indicates the unsubscribe acknowledgement 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 will remove any topic filter that matches exactly 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 to unsubscribe from one or many topic filters.