Diving Into MQTT
Diving Into MQTT
MQTT can make communication in the world of IoT a lot easier. Read on to find out what it is and how to use it.
Join the DZone community and get the full member experience.Join For Free
There are other IoT protocols out there, of course, but MQTT is one of the most efficient.
MQTT was developed around 1999 with the main goal of creating a very bandwidth-efficient protocol. Moreover, it's not very power-intensive, making it even more attractive for IoT.
This protocol uses a publish-subscriber paradigm, in contrast to the HTTP-based request/response paradigm. It uses binary messages to exchange information with low overhead. It is very simple to implement, and, even better, it is open. Another interesting aspect is the fact that MQTT uses a TCP stack as transmission substrate. All these facts contribute to its large adoption in IoT.
MQTT Message Pattern
As mentioned, MQTT implements the publish-subscriber paradigm. This paradigm decouples a client that publishes a message (“publisher”) to other clients that receive the message (“subscribers”). Moreover, MQTT is an asynchronous protocol, which means that it does not block the client while it waits for the message. Of course, that's in contrast to HTTP, which is mainly a synchronous protocol. Another interesting property of MQTT protocol is that it does not require that the client (“subscriber”) and the publisher are connected at the same time.
MQTT Publisher-Subscriber Pattern
As described above MQTT is a message based protocol that uses publisher-subscriber pattern. The key component in MQTT is the MQTT broker. The main task of MQTT broker is dispatching messages to the clients (“subscribers”). In other words, it receives messages from publisher and dispatches these messages to the subscribers. While it dispatches messages, the MQTT broker uses the topic to filter the clients that will receive the message. The topic is a string, and it is possible to combine the topics creating topic levels.
A topic is like a virtual channel that connects a publisher to its subscribers. This topic is managed by the MQTT broker. Through this virtual channel, the publisher is decoupled from the subscribers, and the clients (publishers or subscribers) does not have to know each other. This makes this protocol highly scalable without a direct dependency from the message producer (“publisher”) and the message consumer (“subscriber”).
The schema below describes the MQTT architecture:
How to Use MQTT
Now that we have an overview, it's time to learn how to use it with a real example. There are several implementations of MQTT, but in this example, we will use Mosquitto, an implementation developed by Eclipse. The first step is installing the MQTT broker, and for our purposes, we will install it on a Raspberry PI. To install Mosquitto, we have to add the repository that holds the application so that we can download it. Before adding the repository, it is necessary to add the key to verify that the download package is valid. Let's connect to our Raspberry Pi using SSH or a remote desktop connection. Now, in the terminal, we have to write:
Now it's time to import the key:
sudo apt-key add mosquitto-repo.gpg.key
Finally, let us add the file .list:
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
With that, the MQTT server is installed on the Raspberry PI. This server is our MQTT broker, as specified above. Now we need to install the client, or in other words, the publisher and the subscriber. In this example, we will install client and server on the same Raspberry, but you can install it on a different PC/server or IoT board.
apt-get install mosquitto-clients
How to Send an MQTT Message
We have installed and configured the client and the server, now we can register a subscriber to a specific topic and wait for incoming message from a publisher. To register a subscriber we will use this command:
As you can can see, our subscriber is waiting for a message. In this example, we used a topic called swa_news. Now we will send a message using a MQTT publisher that uses that same topic.
In the example, the MQTT publisher sends “Hello Protocol” message. On the subscriber side, we get the message:
How to Use MQTT on an Android Device
In this last example, we will use an Android MQTT client so that it receives the message we sent. This video shows how to configure an Android MQTT client.
At the end of this post, you've hopefully learned what the MQTT protocol is and how to publish a message to clients.
Published at DZone with permission of Francesco Azzola , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.