Why is everyone talking about MQTT?

Ably just released official support for the MQTT protocol. Through this article, I aim to explain the protocol itself and why it’s a game-changer for making realtime IoT applications.

Ably now officially supports the MQTT protocol via it’s MQTT Protocol Adapter

What is MQTT exactly?

Message Queuing Telemetry Transport (MQTT), is a network protocol especially built to enable efficient communication to and from the 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’s 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 a broker.

An illustration of MQTT’s publisher and subscriber clients along with a broker

As seen above, MQTT has three main entities with one basic concept.

Entities:

  • 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).

Concept:

  • 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 on 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 endpoint intended to connect with a third party protocol to one that is compatible to work with Ably.

An illustration of Ably’s MQTT Protocol Adapter

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 specially 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 quite easy, just go through the docs and follow the simple steps in order to get started quickly.

You can also directly see the MQTT Protocol Adapter in action by following this tutorial that lets you build the old-school but super fun game of snake.

One important thing to consider when using the MQTT adapter 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 guarantee the same level of security as the API key is fairly exposed. For this reason, we highly recommend using the MQTT protocol adapter over the Token Authentication scheme only. However, if your application must implement Basic Authentication, we support that as well, but enforce that the connection is secured using SSL.

However, the MQTT adapter is only recommended to be used when:

  • Using devices that 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
Srushtika is a Developer Advocate for Ably Realtime

What will you build with Ably’s MQTT protocol adapter? Reply to this post or tweet it to us!

--

--

Srushtika Neelakantam
Ably: Serious, serverless realtime infrastructure

Senior Product Manager for Ably Realtime | Mozilla Tech Speaker and Rep Alumnus