Ably just released official support for the MQTT protocol via our [MQTT gateway](https://www.ably.io/adapters). Through this article, I aim to explain the protocol itself and why it’s a game changer for making realtime IoT applications.
Update June 2019: we now support SSE too!
What is MQTT exactly?
Message Queuing Telemetry Transport (MQTT), is a network protocol especially built to enable efficient communication to and from Internet of Things (IoT) devices.
Additionally, unlike the most popular networking protocol HTTP, MQTT does not follow the request/response mechanism for communication. Instead, it uses the publish/subscribe paradigm (popularly known as Pub/Sub).
What is Pub/Sub?
Pub/Sub is a messaging pattern where all the clients of a service are either publishers or subscribers, or both. There’s a central entity that manages the message routing from publishers to the correct subscribers. While the publishers push the data/messages to this central entity, the subscribers inform this central entity that they are interested in a certain kind of data and whenever the central entity has that data, it forwards it to the concerned subscribers. This central entity may be a platform or a server but in MQTT terminology, it’s called an [MQTT broker](https://www.ably.io/adapters).
As seen above, MQTT has three main entities with one basic concept.
- Publishers- publish messages to the broker.
- Subscribers- subscribe to a particular topic with the broker.
- Broker- ensures correct routing of messages published to it by the publishers, to the correct subscriber(s).
- Topics- can be thought of as naming specific sets of data. It’s generally a plain string, usually corresponds to a legit word in English for ease of use. A publisher would essentially publish data to one of the topics. Similarly, a subscriber can subscribe to a particular topic. The job of the broker is to route messages based on the topic.
But how do MQTT and Ably work out?
Ably has a service layer which we call Protocol Adapters. It essentially serves as a way to use third party protocols and platforms with Ably. Consider it as an internal translation layer that translates the client side’s end point intended to connect with a third party protocol to one that is compatible to work with Ably.
MQTT vs WebSockets
While the WebSockets protocol continues to pioneer the world of realtime technology, MQTT caters to a particular area of realtime communication between IoT devices. So under what circumstances would MQTT be preferred?
- It is especially designed to work in low bandwidth scenarios
- It implements the Pub/Sub messaging pattern right at the protocol level
- It ensures efficient communication while consuming the least possible battery on hardware devices
All these benefits make MQTT the best available protocol when considering the connected devices future a.k.a, communication between IoT devices.
How to use Ably’s MQTT Protocol Adapter
Setting up the MQTT protocol adapter with Ably is simple, just go through the docs and follow the steps in order to quickly get started.
You can also directly see the MQTT Protocol Adapter in action by following this MQTT tutorial that let’s you build the old-school but super fun game of Snake.
One important thing to consider when using the [MQTT adapter](https://www.ably.io/adapters) is the authentication scheme. Unlike your server side clients, which are generally secure as there is no end user interaction, IoT devices on the other hand (or any client side applications in general) cannot gurantee the same level of security as the API key is pretty exposed. For this reason, we highly recommend using the MQTT protocol adapter over Token Authentication scheme only. However, if your application must implement Basic Authentication, we support that as well, but enforce that the connection be secured using SSL.
However, the MQTT adapter is only recommended to be used when:
- Using devices which do not have a native Ably client library, such as Arduino platforms, C/C++ applications, and so on
- The bandwidth is limited and you want to keep network traffic to a minimum
What will you build with Ably’s MQTT protocol adapter? Reply to this post or tweet it to us!