Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MQTT Essentials - A Lightweight IoT Protocol

You're reading from   MQTT Essentials - A Lightweight IoT Protocol Send and receive messages with the MQTT protocol for your IoT solutions.

Arrow left icon
Product type Paperback
Published in Apr 2017
Publisher Packt
ISBN-13 9781787287815
Length 280 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (8) Chapters Close

Preface 1. Lightweight Messaging with MQTT 3.1.1 and Mosquitto FREE CHAPTER 2. Securing an MQTT Mosquitto Server 3. Controlling a Drone with Python 4. Interacting with Sensors in Java 5. Controlling Home Automation in JavaScript 6. Interacting with Actuators in Swift A. Exersise Answers

Working with best practices when creating topics

We already know that MQTT allows us to publish messages on topics. A publisher always has to to specify the topic name to which a message will be published. The easiest way to understand topic names in MQTT is to think about them like paths in a file system.

If we have to save dozens of files that have information about different types of sensors for a diverse number of drones, we can create a hierarchy of directories or folders to organize all the files that we will save. We can create a directory named sensors, then one sub-directory for each drone, such as drone01, and finally a sub-directory with the sensor name, such as altitude. The path in macOS or Linux will be sensors/drone01/altitude because these operating systems use a forward slash (/) as a delimiter. In Windows, the path will be sensorsdronealtitude because this operating system uses a backslash () as a delimiter.

Then, we will save the files that have information about the altitude sensor for the drone named drone01 in the created path. Instead of saving files in a path, we can think about publishing a message to a path and use the same mechanism we use to organize files in paths to arrange messages in topics.

Instead of directories or folders, a topic has topic levels, specifically, a hierarchy of topic levels and the slashes (/) are used as delimiters, that is, topic level separators. If we use sensors/drone01/altitude as the topic name, sensors is the first topic level, drone01 is the second topic level and altitude is the third topic level.

Tip

Topic names are case-sensitive, and therefore, sensors/drone01/altitude is different from sensors/Drone01/altitude, Sensors/drone01/altitude and Sensors/Drone01/Altitude. In fact, the four strings will be considered as four individual topic names. We must make sure we select a casing scheme for the topic names and we use it for all the topic names and topic filters.

We can use any UTF-8 character in topic names with the exception of the two wildcard characters that we will analyze later: the plus sign (+) and hash (#). Hence, we must avoid + and # in the topic names. However, it is a good practice to restrict the character set to avoid unexpected problems with client libraries. For example, we can avoid accents and characters that are not common in English as we do whenever we build URLs. It is possible to use these characters but be sure that you can face issues when using them.

We should avoid creating topics starting with the dollar sign ($) because many MQTT servers publish statistics data, related to the servers, in topics that start with $. Specifically, the first topic level is $SYS.

We must maintain consistency when sending messages to different topic names as we do when we save files in different paths. For example, if we want to publish the altitude for a drone named drone20, we will use sensors/drone20/altitude. We must use the same topic levels that we used for the same goal for drone01 and just change the drone name from drone01 to drone20. It would be a really bad practice to use a topic with a different structure or inconsistent casing, such as altitude/sensors/drone20 or Sensors/Drone20/Altitude. We have to take into account that we can subscribe to multiple topics by using topic filters, and therefore, it is very important to create topic names accordingly.

You have been reading a chapter from
MQTT Essentials - A Lightweight IoT Protocol
Published in: Apr 2017
Publisher: Packt
ISBN-13: 9781787287815
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime