IoT, Technical Stuff

Mosquitto, a brief exploration into it

mosquittologo

What is Mosquitto?

Mosquitto is an open source (EPL/EDL licensed) message broker written in C that implements the MQ Telemetry Transport protocol (MQTT) versions 3.1 and 3.1.1. It’s an iot.eclipse.org project.
MQTT is a machine-to-machine (M2M)/”Internet of Things” connectivity protocol. It carries a lightweight publish/subscribe messaging transport model. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium, therefore is suitable for “machine to machine” messaging such as with low power sensors, mobile devices such as phones, embedded computers or microcontrollers like the Arduino.

What is Mosquitto used for?

The internet has connected high-resource devices with a lot of power, memory and connection options. The protocols supporting these devices are considered too heavy to apply to the upcoming IoT world, with its applications and constrained devices. But this internet of things world is making real the possibility of a smarter planet therefore the need to supporting it is becoming a must. Supporting it, is relatively new and every day are emerging advance approaches to telemetry that are making possible to connect all kind of devices, wherever they might be, to each other, to the internet and to the business enterprise. One of these advancements is the MQTT messaging protocol. It is so lightweight that it can be supported by some of the smallest measuring and monitoring devices and transmit data over widespread, sometimes intermittent networks. It is also open source, which makes it easy to adapt to a variety of messaging and communication needs.

Telemetry technology allows things to be measured or monitored from a distance, however the challenges lie in getting the information from the devices to the people and applications that want to use it, first, in time to use it effectively and second, with the added ability for them to reply to the devices with new instructions or requests. If the devices are widely distributed geographically or if they have limited storage or computational abilities, the challenges increase considerably, as do costs. Fortunately, these challenges are being overcome through the use of improved telemetry technologies and communication protocols that are making it possible to send and receive this information reliably over the Internet, even if the network is unsteady or the monitoring device has little processing power. MQTT provides telemetry technology to meet the information challenges of today’s Internet users. Within this context, an MQTT broker is a server that implements the MQTT protocol. It mediates communication between MQTT client applications, such as those running in remote sensors and other devices, and the enterprise integration layer. So, basically Mosquitto is the communication between MQTT client applications and is one of the most popular MQTT based message brokers.

MQTT benefits

MQTT is a publish/susbcribe messaging protocol designed for lightweight M2M communications, originally developed at IBM and now open sourced. Some of its benefits include:

  • Extends connectivity beyond enterprise boundaries to smart devices.
  • Offers connectivity options optimized for sensors and remote devices.
  • Delivers relevant data to any intelligent, decision-making asset that can use it.
  • Enables massive scalability of deployment and management of solutions.

Now, the Mosquitto project provides a small server implementation of the MQTT and MQTT-SN protocols. Small means that:

  1. Only necessary function is included (conditional compilation can be used to be able to omit unneeded function for a particular application).
  2. That function is coded as efficiently as possible.
  3. The externals are as simple as possible for the function provided.

The server (mosquitto) by its own has the following features, which are not described in the MQTT specification:

  1. An MQTT bridge, to allow Mosquitto to connect to other MQTT servers.
  2. The ability to secure communications using SSL/TLS.
  3. User authorization – the ability to restrict user access to MQTT topics.

Mosquitto architecture (MQTT functioning)

MQTT uses a publish/subscribe messaging pattern that enables a loose coupling between the information provider, called the publisher, and consumers of the information, called subscribers. This is achieved by introducing a message broker between the publisher and the subscribers. Compared with the traditional point-to-point pattern, the advantage of the publish/subscribe model is that the publishing device or application does not need to know anything about the subscribing one, and vice-versa. The publisher simply sends the message with an identifier that denotes its topic, or subject area. The broker then distributes the message to all applications or devices that have chosen to subscribe to that topic. In this way, the publish/subscribe pattern turns traditional point-to-point messaging into a multicast of content-based communications. Each publisher and subscriber focuses only on sending and receiving the information they care about, while the broker sits between them and routes each message in the proper direction based on its topic designation.

architecture-mosquitto

MQTT brokers comparison

  • Mosquitto – Mosquitto is one of the most popular MQTT brokers and has one of the longest pedigrees. It is included with almost all Linux distros and is pretty straightforward to compile and install. Being Mosquitto a written in C laguage broker, makes it a sweet option to run in machines/devices that doesn’t afford the capacity of running a JVM.
  • VerneMQ – It bills itself as a high-performance, distributed message broker. Also as the most scalable and robust MQTT message broker for IoT, M2M, Mobile and WebApplications. At this level, compared to Mosquitto which bills itself as a lightweight message broker, VerneMQ is a competitor designed to be a robust solution that won’t fit easily to the targeting constrained devices and networks that Mosquitto is focused on.
  • Moquette – Moquette is a simple and small Java implementation of an MQTT 3.1 broker. Its code base is small and at its core is an events processor. Moquette broker is lightweight and easy to understand so it could be embedded in other projects. By default it lives standalone, but could be integrated into an OSGi container to create more significant integrations. It’s part of the Eclipse Foundation and it’s major strength is that is a freely available pure MQTT broker written in Java language. That’s also the main contribution to the Eclipse Foundation cause Moquette adds to the Foundation a Java implementation of MQTT, different than Mosquitto which is a C language implementation. This is a win-win for Eclipse Foundation and the growing IoT community.
  • emqttd – emqttd (Erlang MQTT Broker) bills itself as a massively scalable and clusterable MQTT V3.1/V3.1.1 broker written in Erlang/OTP. Is fully open source and licensed under the Apache Version 2.0. It implements both MQTT V3.1 and V3.1.1 protocol specifications and supports WebSocket, STOMP, SockJS, CoAP and MQTT-SN at the same time. The fact that is an MQTT broker based does not means it is lightweight as a broker, it means that is based on a lightweight communication protocol and according to all the information on the emqttd page and the fact that is bills itself as a massively scalable and clusterable broker, it doesn’t seem at least to me to be a lightweight message broker compared to Mosquitto, making the latter a better option for constrained devices and networking.
  • RabbitMQ – It bills itself as a robust messaging for applications, easy to use and supporting a huge number of developer platforms. It’s a AQMP message broker written in Erlang that has an MQTT plugin available which allow it to support MQTT features but not all of them. If you search about MQTT message brokers, RabbitMQ is one of the populars but compared to Mosquitto it has a limited number of MQTT supported features and is not designed for constrained devices and networking.
  • ActiveMQ – It bills itself as a popular, powerful and fast messaging server that supports many languages and protocols and advanced features with its core written in JMS. It also supports MQTT but is not considered a lightweight message broker but instead a robust one. Being its core JMS it doesn’t support all MQTT features.

mosquittotable

y= supported, n=not supported, ?=unknown

QoS0 – Quality of service, at most one (fire and forget)
QoS1 – Quality of srevice, at least one
QoS2 – Quality of service Exactly one
Authentication – Authentication offering
Bridge – Bridge protocol (client connections to another broker)
$SYS – Space for internal system-monitoring or statistics topics.
SSL – SSL support
Dynamic Topics – MQTT is designed to be zero-admin, therefore topics are expected to be dynamic which means not requiring administrative setup.
Cluster – Cluster support
Websockets – Websockets support
Plugin system – Plugin system offering

When not to use Mosquitto?

Taking a look at the above table, you’ll see that Mosquitto doesn’t support clustering, so when looking for clustering capabilities Mosquitto won’t be an option, but there are others MQTT servers that support clustering that you could implement. And of course, if you want a broker implementing more than just MQTT protocol then you should also take a look at other options since Mosquitto only implements MQTT. Other than that, Mosquitto is a really good MQTT broker implementation that has gained large popularity at the point of being widely supported by the Eclipse community, and that, by itself, says a lot.

Conclusion

The main strength of Mosquitto is its IoT capabilities with constrained devices and networking. In this field is one, if not the most, popular one, competing at least on the lightweight field with Moquette which at the contrary to Mosquitto can work on devices that afford the capacity to run a JVM. All the other options described above are considered more robust. Something to remember is that Mosquitto is a good option but only talking at an MQTT approach which is based over TCP transport layer. There is another popular protocol option when thinking on lightweight messaging, it’s called CoAP. CoAP is a protocol that runs over the UDP transport layer and is growing importance on the IoT world cause is useful to work with constrained devices and constrained networks too. Of course by running natively on a different transport layer, CoAP has different offerings than MQTT. Both are designed for the same target but each one will have its strengths and weaknesses. Californium is an Eclipse project that provides a framework based on CoAP protocol. I’ll try to cover deeply CoAP on a future project.

When communicating devices/machines, we should think first on the kind of device/machine we want to communicate in order to chose the best protocol cause there is a large list of them. After choosing the protocol over the one we will communicate our devices/machines, then we have to choose the best message broker based on the protocol that suited our needs, although the list is quite large there will be always subtle differences that will help us to take a decision, it won’t be easy and it will require a lot of research but it won’t be impossible. This research now allows me to understand the purpose of Mosquitto and the importance of it on the IoT world, where constrained devices and constrain networks are a common topic.

References

MQTT redbook
MQTT server options
Getting started with MQTT
IoT protocols
Mosquitto, an Eclipse project

Leave a Reply