Exploring RabbitMQ Protocols

Marwen Aouiti
3 min readAug 6, 2023

--

Created with Dalle-E
  1. Introduction:

RabbitMQ is one of the most widely used open-source message brokers. It facilitates the efficient routing and management of messages in distributed systems. As the official documentation says: “RabbitMQ accepts and forwards messages. You can think about it as a post office: when you put the mail that you want posting in a post box, you can be sure that the letter carrier will eventually deliver the mail to your recipient. In this analogy, RabbitMQ is a post box, a post office, and a letter carrier.”

Messaging protocols are essential in defining how these messages are formatted, transmitted, and processed.

In this article, we’ll explore some of the different protocols supported by RabbitMQ and go through code examples to demonstrate each.

2. Protocols:

Advanced Message Queuing Protocol (AMQP)

The Advanced Message Queuing Protocol (AMQP) is an open standard for passing messages between applications or organizations. It ensures that messages are delivered in a platform-neutral and secure way. RabbitMQ’s core functionality is built around AMQP, making it a central topic for developers working with this message broker.

The AMQP model in RabbitMQ is built around key components such as:

  • Exchanges: Routes messages to queues based on rules and binding keys.
  • Queues: Stores messages waiting for processing.
  • Bindings: Defines the relationship between queues and exchanges.

Here’s a simple example to demonstrate publishing and consuming messages using AMQP with RabbitMQ in Python:

var amqp = require('amqplib/callback_api');

// Connect to RabbitMQ
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}

// Create a channel
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}

var queue = 'hello';
var msg = 'Hello AMQP!';

// Declare a queue
channel.assertQueue(queue, {
durable: false
});

// Publish a message
channel.sendToQueue(queue, Buffer.from(msg));
console.log(" [x] Sent 'Hello AMQP!'");

// Consume a message
channel.consume(queue, function(msg) {
console.log(" [x] Received %s", msg.content.toString());
}, {
noAck: true
});
});
});

Message Queuing Telemetry Transport (MQTT)

MQTT is a lightweight messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. It’s particularly popular in IoT (Internet of Things) applications.

RabbitMQ’s MQTT plugin allows it to act as an MQTT broker, bridging the gap between MQTT and AMQP and other messaging protocols.

Here’s an example of publishing and subscribing to messages using the MQTT protocol with RabbitMQ in Node.js:

const mqtt = require('mqtt')
const client = mqtt.connect('mqtt://localhost')

client.on('connect', function () {
client.subscribe('myTopic', function (err) {
if (!err) {
client.publish('myTopic', 'Hello MQTT')
}
})
})

client.on('message', function (topic, message) {
console.log(message.toString())
client.end()
})

Simple (or Streaming) Text Oriented Message Protocol (STOMP)

STOMP is a communication protocol that defines rules for sending and receiving messages between different systems. STOMP is like a translator that helps two computers or systems talk to each other, even if they speak different “languages.” It uses simple text commands (like “SEND” or “SUBSCRIBE”) that are easy to understand. Think of it like texting a friend, but the text messages are commands that tell a computer what to do.

RabbitMQ supports STOMP via a plugin, allowing for seamless integration and providing an option for those looking to use this text-based messaging protocol.

Below is an example to demonstrate working with STOMP and RabbitMQ in Node.js:

const Stomp = require('stomp-client');
const client = new Stomp('127.0.0.1', 61613, 'guest', 'guest');

client.connect(function(sessionId) {
client.subscribe('/queue/myQueue', function(body, headers) {
console.log('Received:', body);
});

client.publish('/queue/myQueue', 'Hello STOMP');
});

HTTP and WebSockets with RabbitMQ

HTTP and WebSockets provide ways to communicate with RabbitMQ over the web. HTTP offers standard web-based access, while WebSockets allow for continuous, bidirectional communication.

Here’s a basic example in Node.js:

const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:15675/ws');

ws.on('open', function() {
ws.send(JSON.stringify({type: 'publish', routing_key: 'test', payload: 'Hello WebSockets', headers: {}}));
});

ws.on('message', function(data, flags) {
console.log(data);
});

Understanding RabbitMQ protocols and working with them effectively is an essential skill for modern application development. This article provided an exploration of AMQP, MQTT, STOMP, and WebSockets with RabbitMQ. By leveraging these protocols, developers can create robust messaging solutions tailored to specific requirements.

--

--