What is MQTT ?
As the Wiki describes it, MQTT (formerly Message Queue Telemetry Transport) is a publish-subscribe based “light weight” messaging protocol for use on top of the TCP/IP protocol.
With the internet having survived using HTTP for the past several years, a question may arise , why another protocol at all ? Well, the answer may be found here.
In spite of being great in matters where a client wants to make a request to a server and get an answer, HTTP doesn’t prove to be a great solution when a source of information should push a change to a number of clients; also there is not built-in support for quality of service in this case. The text based format of HTTP requires more bandwidth and any device acting as a host needs a web server installed. Running a web server to cater to incoming requests obviously consumes hell lot of battery power.
A great way to overcome these drawbacks is to use MQTT. MQTT uses publish and subscribe messaging pattern wherein any source of information (for e.g. a sensor)can publish its data to which any client can be subscribed. A broker keeps track of these publications and subscriptions. So whenever a publisher sends some new data, he publishes a message and the broker takes care of sending the new data to all subscribers. Hence the broker can guarantee the delivery of a message which is why it is said that MQTT is backed by quality of service. It requires less bandwidth (because of its binary format), has small implementation footprint that requires less battery. MQTT is standardized by OASIS and backed by IBM.
To get familiar with the working of MQTT, we need to set up an MQTT broker and in this post , I have selected ‘mosquitto‘ which is the most famous open source MQTT broker. A quick installation and steps for setting it up in Debian have been discussed below.
Installing mosquitto in Debian is a simple task. Open a terminal and paste the following commands
(using the new repository with the latest version of the broker is suggested)
To use the new repository, first the repository package signing key should be imported:
sudo apt-key add mosquitto-repo.gpg.key
Then make the repository available to apt:
sudo wget http://repo.mosquitto.org/debian/mosquitto-stable.list
Then update apt information:
sudo apt-get update
sudo apt-get install mosquitto
Along-with this, also install,a couple of command line clients for testing your installation:
sudo apt-get install mosquitto-clients
In order to start the broker , type the following command in your terminal:
sudo /etc/init.d/mosquitto start
To check the status of the broker, give this command:
sudo /etc/init.d/mosquitto status
Or, check the output of :
netstat -na | grep 1883
1883 is the default mosquito server port.
Subscribing to a topic –
The broker should be up and running on port 1883, assuming that nothing else was running on that port. It can be found out if it is running when the following tests are exectuted. Open 2 terminals. In the first run this command:
mosquitto_sub -d -t hello/world
Here we are subscribing to all messages with the topic “hello/world”
Publishing under a Topic-
To publish a message under a specific topic the following command can be run in the 2nd terminal:
mosquitto_pub -d -t hello/world -m "Hello, MQTT. This is my first message."
This command should have published a message that will be pushed to all clients which have subscribed to the “hello/world” topic.
For e.g. in the case considered, the above command should invoke an output as below in the 1st terminal :
Client mosqsub/12826-cherry sending PINGREQ
Client mosqsub/12826-cherry received PINGRESP
Client mosqsub/12826-cherry received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (38 bytes))
Hello, MQTT. This is my first message.
i.e., the message “Hello, MQTT. This is my first message.” has been pushed to all clients that subscribed to the topic “hello/world”.