Today I will talk about what MQTT is and why you need it in your solutions for the Internet of Things. After a comparison with HTTP, I will go through how MQTT works and what benefits it brings.
With the Internet surviving for such a long time using the HTTP protocol, you might ask yourself why you would ever need another protocol. Well, HTTP is great for doing a request and getting a response, like when a client want to ask a server for some information, and get an answer back. But it doesn’t really have a good solution when a source of information should push a change to many clients, and there is no built-in support for quality of service. The text-based format of HTTP requires more bandwidth, and any device that wants to act as a host needs to have a web server installed. Keeping a web server alive to answer incoming requests takes a lot of battery power, which is a scares resource on any “thing”.
So, let me introduce the Message Queuing Telemetry Transport (MQTT), which is a protocol that can solve many of these problems. First of all it uses a publish-subscribe messaging pattern, which means that any source of information (such as a sensor) can publish its data, and then any client can subscribe to that data. All this is happening in a broker that keeps track of all publications and subscriptions, so when a publisher sends an update with new data (publish a message), the broker takes care of sending the new data to all subscribers. The built-in support for quality of service means that the broker can guarantee delivery of a message, e.g. that it’s delivered at least one time or exactly once. MQTT is a binary format that requires a minimum of bandwidth (the fixed header is only two bytes), and it also has a small implementation footprint that require less battery. Both of these efficient aspects can make MQTT scale very well, and that is important when we are talking about millions of “things”. It’s not as established as HTTP, but it’s standardized by OASIS and backed by IBM.
To see how it works, let’s set up an MQTT broker, and I have selected mosquitto, which is the most famous open source MQTT broker. I will set it up on a cloud server with Ubuntu (12.04 LTS), and to get one of your own, please see my video “Omnichannel Services – Cloud Server”. Start by connecting to the server (replace AwsKeyPar.pem with your key file, and 0.0.0.0 with the IP address of your server)…
ssh -i AwsKeyPair.pem email@example.com
…and then install and run mosquitto using the following commands:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients
Now open a new terminal window, connect to the server (see ssh command above) and enter the command:
mosquitto_sub -d -t hello/world
This starts a subscription to the topic “hello/world”. Now open yet another terminal window, connect to the server, and enter the command:
mosquitto_pub -d -t hello/world -m "This is my first message"
This will publish a first message on the topic “hello/world”, and it should appear in the second terminal windows where you set up the subscription.
So there you have your mosquitto MQTT broker running.
With that I hope you understand what MQTT is and why you need it to build efficient and scalable solutions for the Internet of Things.