![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
MQTT (formerly the MQ Telemetry Transport) is a lightweight protocol that’s primarily designed for connecting power-constrained devices over low-bandwidth networks. Though it existed for over a decade, the advent of M2M (machine to machine communications) and Internet of Things (IoT) made it a popular protocol.
Developers aspiring to build IoT solutions need to learn MQTT, which is quickly becoming the most preferred protocol for connecting devices to the cloud. Enterprise cloud platforms such as Amazon Web Services, Microsoft Azure, and IBM Watson expose their IoT PaaS through MQTT.
This article introduces the core concepts of MQTT that are necessary for building M2M and IoT applications. It covers the basic terminology to advanced concepts, along with a getting started section to see MQTT in action.
Source: Eurotech
MQTT was created way back in 1999 by two engineers — Andy Stanford-Clark (IBM) and Arlen Nipper (Eurotech). They had to invent a new protocol for connecting oil pipelines over unreliable, satellite networks.
The motivation for designing MQTT was to create a lightweight and bandwidth-efficient protocol that was data agnostic with support for multiple levels of Quality of Service (QoS). Interestingly, even today, those are the same reasons for which MQTT is chosen for implementing IoT solutions.
In 2011, IBM and Eurotech donated MQTT to the proposed Eclipse project called Paho. In 2013, it was submitted to OASIS for standardization. The latest version of the protocol specification, 3.11 has become an OASIS standard.
Developers familiar with Message Oriented Middleware (MOM) such as RabbitMQ, Redis, and Apache Qpid may mistake MQTT as yet another implementation of MOM. It’s important to understand that MQTT is just a protocol which has nothing to do with message queues or pub/sub implementations. MQTT cannot replace RabbitMQ or any of the enterprise pub/sub engines.
The fundamental difference between MQTT implementation and MOM is the messages are stored and delivered. Unlike MOM, MQTT is not meant for dealing with durable and persistent messages. It cannot be considered for implementing the store-and-forward pattern. While traditional MOM is designed for reliable delivery of messages among enterprise applications, MQTT is a simpler protocol with just five APIs designed to connect devices. It cannot be used as the middleware for transactional systems.
MQTT uses the pub/sub pattern to connect interested parties with each other. It does it by decoupling the sender (publisher) with the receiver (subscriber). The publisher sends a message to a central topic which has multiple subscribers waiting to receive the message. The publishers and subscribers are autonomous, which means that they do not need to know the presence of each other.
Since MQTT is a specification, it can be supported by existing MOM software. RabbitMQ supports MQTT among other protocols such as HTTP and AMQP. Azure IoT Hub is an example of how an IoT platform supports MQTT.
Let’s understand the terminology of MQTT.
Equipped with the key concepts and terminology, let’s set up the testbed to see MQTT in action. We will install the broker and clients to see the how the messages are published. This will help you understand the essence of MQTT.
The simplest MQTT broker exists in the form of an open source software called Mosquitto. On a Mac with Home Brew, you can install Mosquitto with a single command – brew install mosquitto. For Windows and Linux, please refer to the official installation guide.
The Mosquitto binary is available in /usr/local/sbin. It also needs a configuration file with the settings such as bind address, port, client expiration period, and maximum connections. The default configuration file available at /usr/local/etc/mosquitto/mosquitto.conf can be used without any modifications.
To launch Mosquitto on a Mac, run the following command:
/usr/local/sbin/mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
This will launch Mosquitto with the default settings. You should see output similar to the following:
Now that the broker is up and running let’s launch the clients which all act as the publisher and subscribers. Though Mosquitto comes with command line tools for pub/sub, it is fun to use a GUI. Let’s download Java-based MQTT client called MQTT.fx from http://mqttfx.jfx4ee.org.
Create two folders on your Mac called pub and sub and then copy MQTT.fx application from the downloaded DMG image to both the folders. Each copy represents the publisher and subscriber respectively.
Launch both the copies of the MQTT.fx application and configure them to talk to the local Mosquitto broker. Click on the gear icon to configure the connection settings. Click the + symbol in the Connection Profiles window to add a profile that connects to the Mosquitto running on the localhost.
Make sure that you use unique client ids for the publisher and subscriber. Connect both the instances of MQTT.fx to the Mosquitto broker. Click on the Subscribe tab of subscriber application, and type test/topic1 before clicking the Subscribe button. In the publisher, add the same topic, test/topic1, and type a message in the text box below it.
You should see the messages flowing from the publisher to the subscriber. You can launch additional subscribers that listen on the same topic. Finally, switch to the command window to see Mosquitto acknowledging the client events.
We have successfully set up the broker, publisher, and subscriber on our local machine. We have also seen how to publish messages to the subscribers.
MQTT is the most preferred protocol for M2M and IoT applications. Based on the pub/sub pattern, it simplifies the connectivity between devices. This article attempted to introduce you to the basics of MQTT. We will cover the advanced concepts of MQTT including QoS, security, LWT, persistence, and the integration with WebSockets in the upcoming articles.