Publishing messages with a GUI tool

Now, we will use the MQTT.fx GUI utility to generate another MQTT client that publishes another message to the same topic, 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. Enter the following text in the textbox below the Publish button: 32 f, as shown in the following screenshot:
  1. Then, click the Publish button. MQTT.fx will publish the entered text to the specified topic.

If you don't want to work with the MQTT.fx utility, you can run another 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 "32 f"

Now, 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 ones:

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

If we clean up debug messages that start with the Client prefix, we will see just the next two lines. These lines show the payloads for the two messages that we received as a result of our subscription to the sensors/octocopter01/altitude topic:

25 f
32 f

Go to the MQTT.fx window and click Subscribe. You will see 2 at the right-hand side of the title for the topic filter used to subscribe in the panel located at the left-hand side of the window. MQTT.fx is telling you that you have received two messages in the sensors/octocopter01/altitude topic. Click on this panel and MQTT.fx will display all the received messages on the right-hand side of the panel. MQTT.fx will display a number at the right-hand side of each message to specify the message number since we started the subscription to the topic filter. Click on each message and MQTT.fx will display the QoS level for the message (0), the date and time it was received, and the payload for the message in the default plain string format. The following screenshot shows the payload for the second message that has been received by the subscriber generated by MQTT.fx:

We created two publishers and each of them published a message to the same topic: sensors/octocopter01/altitude. The two subscribers for this topic received both messages. Now, we will understand what happens under the hood when a client publishes a message to a topic.

The MQTT client that has already established a connection sends a PUBLISH packet to the MQTT server with a header that includes the following fields and flags. We need to understand the meaning of these fields and flags because we will be able to specify some of their values when we work with MQTT tools and MQTT client libraries in Python:

  • PacketId: If the QoS level is equal to 0, the value for this field will be 0 or it won't be present. 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.
  • Dup: If the QoS level is equal to 0, the value for this field will be 0. If the QoS level is equal to 1 or 2, the MQTT client library or the MQTT server can resend a message that was previously published by the client when the subscribers haven't acknowledged the first message. Whenever there is an attempt to resend a message that has already been published, the value for the Dup flag must be 1 or True.
  • QoS: Specifies the QoS level for the message. We will dive deep into the quality of service level for messages, and their relationship with many other flags, later. So far, we have been working with QoS level 0.
  • Retain: If the value for this flag is set to 1 or True, the MQTT server will store the message with its specified QoS level. Whenever new MQTT clients subscribe to a topic filter that matches the topic for the stored or retained message, the last stored message for this topic will be sent to the new subscriber. If the value for this flag is set to 0 or False, the MQTT server won't store the message and won't replace a retained message with the same topic if there is one message retained for this topic.
  • TopicName: A string with the topic name to which the message must be published. Topic names have a hierarchy structure where slashes (/) are used as delimiters. In our examples, the value for TopicName was "sensors/octocopter01/altitude". We will analyze best practices for topic names later.

The payload contains the actual message that the MQTT client wants the MQTT server to publish. MQTT is data-agnostic, and therefore we can send any binary data and we don't have restrictions such as those imposed by JSON or XML. Of course, we can use these or others to organize payloads if we wish. In our examples, we sent a string that included a number that represented the altitude, followed by a space, and an "f" that indicates the unit of measurement is feet.

The MQTT server will read a valid PUBLISH packet and it will respond with a packet only for QoS levels greater than 0. If the QoS level is 0, the MQTT server will not respond. The MQTT server will identify all subscribers whose subscribed topic matches the topic name specified for the message and the server will publish the message to these clients.

The following diagram shows the interaction between an MQTT client and an MQTT server to publish a message with a QoS level of 0:

The other QoS levels have a different flow with an additional interaction between the publisher and the MQTT server, and increased overheads that we will analyze later.

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

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