Open source messaging middleware
RabbitMQ

© Lead Image © Tatiana Tushyna, 123RF.com
Connect multiple protocols and servers together on your IoT projects.
For Internet of Things (IoT) projects, there are a lot of different ways that sensors, devices, and client interfaces can be connected together. For many projects, using a simple Message Queue Telemetry Transport (MQTT) broker is all that you need. However, if you're trying to merge and build IoT projects that use both MQTT and Advanced Message Queue Protocol (AMQP), or you require a REST API, then you should take a look at RabbitMQ.
RabbitMQ [1] is an open source middleware solution that natively uses AMQP communications, but it has a good selection of plugins to support features like MQTT, MQTT WebSockets, HTTP REST API, and server-to-server communications (Figure 1).

In this article, I will set up a RabbitMQ server, and I will look at some of the differences between MQTT and AMQP messaging. Finally, an example of an Arduino MQTT message will be presented as both an MQTT and an AMQP item in a Node-RED dashboard.
Getting Started Locally
RabbitMQ can be installed on Windows, Linux, and macOS systems, and there are also some cloud-based offerings. For small systems, lower-end hardware like a Raspberry Pi can be used. Complete RabbitMQ installation instructions are available online [2]. To install and run RabbitMQ on an Ubuntu system, enter:
sudo apt-get update sudo apt-get install rabbitmq-server sudo service rabbitmq-server start
The next step is to add some plugins. For my project, I loaded the MQTT and web administration plugins:
sudo rabbitmq-plugins enable rabbitmq_mqtt sudo rabbitmq-plugins enable rabbitmq-management
The rabbitmqctl
command-line tool allows you to configure and review the RabbitMQ server. To add a user (admin1) with a password (admin1) that has config, write, and read rights for management and administrator access, enter:
sudo rabbitmqctl add_user admin1 admin1 sudo rabbitmqctl set_permissions -p / admin1 ".*" ".*" ".*" sudo rabbitmqctl set_user_tags admin1 management administrator
After you've defined an administrative user, the RabbitMQ web management plugin can be accessed at http://ip_address:15672 (Figure 2).

The RabbitMQ web management tool offers an overview of the present system load, connections, exchanges, and queues.
The RabbitMQ web management tool is excellent for small manual changes. However if you are looking at a doing a large number of additions or changes, then rabbitmqadmin
, you can use the command-line management tool. Install the tool by entering:
# Get the cli and make it available to use. wget http://127.0.0.1:15672/cli/rabbitmqadmin sudo chmod +x rabbitmqadmin
Comparing MQTT and AMQP
It's useful to comment about some of the differences between MQTT and AMQP.
MQTT is a lightweight publish- and subscribe-based messaging protocol that works well with lower-end hardware and limited bandwidth. For Arduino-type applications where you only need to pass some sensor data, MQTT is an excellent fit.
AMQP has more overhead than MQTT, because it is a more advanced protocol that includes message orientation, queuing, routing, reliability, and security. Presently, there are no mainstream AMQP Arduino libraries, but numerous programming options for Raspberry Pi, Linux, Windows, and macOS systems exist. An AMQP IoT example would be to send sensor failures and alarms to dedicated maintenance and alarm queues.
MQTT and AMQP Queues
One of the biggest differences in queues is that MQTT queues are designed to show you the last available message, where as AMQP will store multiple messages in a queue.
A published MQTT message contains a message body, a retain flag, and a quality of service (QoS) value.
An AMQP message can be published with added properties, such as time stamp, type of message, and expiration information. AMQP messages also support the addition of custom header values. Listing 1 is a Python publish example that defines the message type to be "Pi Sensor"
, and it includes custom headers for status and alarm state.
Listing 1
Python AMQP Publish Example
01 #!/usr/bin/env python 02 import pika 03 04 node = "192.168.0.121" 05 user = "pete" 06 pwd = "pete" 07 08 # Connect to a remote AMQP server with a username/password 09 credentials = pika.PlainCredentials(user, pwd) 10 connection = pika.BlockingConnection(pika.ConnectionParameters(node, 11 5672, '/', credentials)) 12 channel = connection.channel() 13 14 # Create a queue if it doesn't already exist 15 channel.queue_declare(queue='Rasp_1',durable=True) 16 17 # Define the properties and publish a message 18 props = pika.BasicProperties( 19 headers= {'status': 'Good Quality',"alarm":"HI"}, 20 type ="Pi Sensor") 21 channel.basic_publish(exchange='', 22 routing_key='Rasp_1',body='99.5', properties = props) 23 24 connection.close()
The results from the Listing 1 example can be examined using the Queue | Get Message option in the RabbitMQ web management interface (Figure 3).

Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Direct Download
Read full article as PDF:
Price $2.95
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Find SysAdmin Jobs
News
-
CarbonOS: A New Linux Distro with a Focus on User Experience
CarbonOS is a brand new, built-from-scratch Linux distribution that uses the Gnome desktop and has a special feature that makes it appealing to all types of users.
-
Kubuntu Focus Announces XE Gen 2 Linux Laptop
Another Kubuntu-based laptop has arrived to be your next ultra-portable powerhouse with a Linux heart.
-
MNT Seeks Financial Backing for New Seven-Inch Linux Laptop
MNT Pocket Reform is a tiny laptop that is modular, upgradable, recyclable, reusable, and ships with Debian Linux.
-
Ubuntu Flatpak Remix Adds Flatpak Support Preinstalled
If you're looking for a version of Ubuntu that includes Flatpak support out of the box, there's one clear option.
-
Gnome 44 Release Candidate Now Available
The Gnome 44 release candidate has officially arrived and adds a few changes into the mix.
-
Flathub Vying to Become the Standard Linux App Store
If the Flathub team has any say in the matter, their product will become the default tool for installing Linux apps in 2023.
-
Debian 12 to Ship with KDE Plasma 5.27
The Debian development team has shifted to the latest version of KDE for their testing branch.
-
Planet Computers Launches ARM-based Linux Desktop PCs
The firm that originally released a line of mobile keyboards has taken a different direction and has developed a new line of out-of-the-box mini Linux desktop computers.
-
Ubuntu No Longer Shipping with Flatpak
In a move that probably won’t come as a shock to many, Ubuntu and all of its official spins will no longer ship with Flatpak installed.
-
openSUSE Leap 15.5 Beta Now Available
The final version of the Leap 15 series of openSUSE is available for beta testing and offers only new software versions.