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.