MQTT

This page provide you with all the MQTT basics and is a part of the CloudAMQP documentation, which offers a hosted MQTT solution.

MQ Telemetry Transport is a publish-subscribe pattern-based "lightweight" messaging protocol. The protocol is often used in the IoT (Internet of Things) world of connected devices. It is designed for built-in systems, mobile phones, and other memory and bandwidth sensitive applications.

Publish subscribe example

MQTT provides an asynchronous communications protocol; the sender and receiver of the message do not need to interact with the message queue, the topic, at the same time. Messages placed onto the topic are stored until the recipient retrieves them or the messages time out. MQTT is for good use by bandwidth-sensitive applications.

MQTT overview

CloudAMQP hosts two brokers, LavinMQ and RabbitMQ, both supporting multiple protocols, including MQTT.

MQTT Protocol on LavinMQ

MQTT version 3.1 is supported in LavinMQ with no plugin needed. QOS levels 0 and 1 are available. Port 1883 is used for MQTT. In the connect packet, client_id, username, password, clean_session, will, and qos are required. More detailed configuration options and client examples can be found in the LavinMQ MQTT documentation.

MQTT protocol on RabbitMQ

The MQTT plugin is available on all plans.

The MQTT plugin needs to be enabled on dedicated plans. Activate the plugin from the Plugin page. The port used for MQTT is 1883 (8883 for TLS wrapped MQTT). Use the same default username and password as for AMQP. When signing in, add the vhost to the username. The vhost is the same as the username for shared plans:
vhost:username

QoS 1 is supported (if you are setting QoS 2, it will downgrade to QoS 1).

Learn more about MQTT for RabbitMQ in the official documentation: RabbitMQ MQTT Plugin

Language-specific documentation can be found here: Ruby, Python, NodeJS, Java, GO, .NET, NodeMCU (Lua), PHP.

Example when using mosquitto and TLS on shared instances:

//Subscribe
mosquitto_sub -h hostname -d -t topic -u my_vhost:my_username -P my_password -c -i test1 -q 1 --cafile /usr/local/etc/openssl/cert.pem -p 8883

//Publish
mosquitto_pub -h hostname -d -m message -t topic -u my_vhost:my_username -P password

Comparing MQTT and AMQP

AMQP and MQTT are both messaging protocols but serve different purposes. AMQP uses exchanges to route messages to queues based on bindings, while MQTT relies on topics for message distribution. MQTT is lightweight and ideal for IoT devices, whereas AMQP offers more advanced routing and reliability features for enterprise systems. QoS levels in MQTT determine message delivery guarantees, while AMQP supports transactions and acknowledgments.

Read the full comparison: AMQP vs MQTT

When an MQTT client connects to RabbitMQ or LavinMQ and subscribes to a topic, the broker will create a queue for the client and bind it to the exchange. Messages will then be routed to the client's queue and delivered to the client. If the client subscribes to multiple topics, more bindings will be added to the queue. If both QoS 0 and 1 are used, the broker will create two queues, one for each QoS.

The default naming schema for MQTT queues are mqtt-subscription-.

Restrict user access client ID

Set the following permissions for the user to restrict access to only their own client ID:

configure ^mqtt-subscription-USERNAMEqos[01]$
write ^(mqtt-subscription-USERNAMEqos[01]|amq\.topic)$
read ^mqtt-subscription-USERNAMEqos[01]$

More reading

If you have any questions, please feel free to contact support@cloudamqp.com for further assistance.