RabbitMQ tutorial - Routing
Routing
(using the amqp.node client)
Prerequisites
This tutorial assumes RabbitMQ is installed and running on
localhost on the standard port (5672). In case you
use a different host, port or credentials, connections settings would require
adjusting.
Where to get help
If you're having trouble going through this tutorial you can contact us through GitHub Discussions or RabbitMQ community Discord.
In the previous tutorial we built a simple logging system. We were able to broadcast log messages to many receivers.
In this tutorial we're going to add a feature to it - we're going to make it possible to subscribe only to a subset of the messages. For example, we will be able to direct only critical error messages to the log file (to save disk space), while still being able to print all of the log messages on the console.
Bindings
In previous examples we were already creating bindings. You may recall code like:
channel.bindQueue(q.queue, exchange, '');
A binding is a relationship between an exchange and a queue. This can be simply read as: the queue is interested in messages from this exchange.
Bindings can take an extra binding key parameter (the empty string in the code above). This is how we could create a binding with a key:
channel.bindQueue(queue_name, exchange_name, 'black');
The meaning of a binding key depends on the exchange type. The
fanout exchanges, which we used previously, simply ignored its
value.
Direct exchange
Our logging system from the previous tutorial broadcasts all messages to all consumers. We want to extend that to allow filtering messages based on their severity. For example we may want the script which is writing log messages to the disk to only receive critical errors, and not waste disk space on warning or info log messages.
We were using a fanout exchange, which doesn't give us much
flexibility - it's only capable of mindless broadcasting.
We will use a direct exchange instead. The routing algorithm behind
a direct exchange is simple - a message goes to the queues whose
binding key exactly matches the routing key of the message.
To illustrate that, consider the following setup:
In this setup, we can see the direct exchange X with two queues bound
to it. The first queue is bound with binding key orange, and the second
has two bindings, one with binding key black and the other one
with green.
In such a setup a message published to the exchange with a routing key
orange will be routed to queue Q1. Messages with a routing key of black
or green will go to Q2. All other messages will be discarded.
Multiple bindings
It is perfectly legal to bind multiple queues with the same binding
key. In our example we could add a binding between X and Q1 with
binding key black. In that case, the direct exchange will behave
like fanout and will broadcast the message to all the matching
queues. A message with routing key black will be delivered to both
Q1 and Q2.
Emitting logs
We'll use this model for our logging system. Instead of fanout we'll
send messages to a direct exchange. We will supply the log severity as
a routing key. That way the receiving script will be able to select
the severity it wants to receive. Let's focus on emitting logs
first.
As always, we need to create an exchange first:
var exchange = 'direct_logs';
channel.assertExchange(exchange, 'direct', {
durable: false
});
And we're ready to send a message:
var exchange = 'direct_logs';
channel.assertExchange(exchange, 'direct', {
durable: false
});
channel.publish(exchange, severity, Buffer.from(msg));
To simplify things we will assume that 'severity' can be one of
info, warning, or error.
Subscribing
Receiving messages will work just like in the previous tutorial, with one exception - we're going to create a new binding for each severity we're interested in.
args.forEach(function(severity) {
channel.bindQueue(q.queue, exchange, severity);
});
Putting it all together
The code for emit_log_direct.js script:
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var exchange = 'direct_logs';
var args = process.argv.slice(2);
var msg = args.slice(1).join(' ') || 'Hello World!';
var severity = (args.length > 0) ? args[0] : 'info';
channel.assertExchange(exchange, 'direct', {
durable: false
});
channel.publish(exchange, severity, Buffer.from(msg));
console.log(" [x] Sent %s: '%s'", severity, msg);
});
setTimeout(function() {
connection.close();
process.exit(0)
}, 500);
});
The code for receive_logs_direct.js:
#!/usr/bin/env node
var amqp = require('amqplib/callback_api');
var args = process.argv.slice(2);
if (args.length == 0) {
console.log("Usage: receive_logs_direct.js [info] [warning] [error]");
process.exit(1);
}
amqp.connect('amqp://localhost', function(error0, connection) {
if (error0) {
throw error0;
}
connection.createChannel(function(error1, channel) {
if (error1) {
throw error1;
}
var exchange = 'direct_logs';
channel.assertExchange(exchange, 'direct', {
durable: false
});
channel.assertQueue('', {
exclusive: true
}, function(error2, q) {
if (error2) {
throw error2;
}
console.log(' [*] Waiting for logs. To exit press CTRL+C');
args.forEach(function(severity) {
channel.bindQueue(q.queue, exchange, severity);
});
channel.consume(q.queue, function(msg) {
console.log(" [x] %s: '%s'", msg.fields.routingKey, msg.content.toString());
}, {
noAck: true
});
});
});
});
If you want to save only 'warning' and 'error' (and not 'info') log messages to a file, just open a console and type:
./receive_logs_direct.js warning error > logs_from_rabbit.log
If you'd like to see all the log messages on your screen, open a new terminal and do:
./receive_logs_direct.js info warning error
# => [*] Waiting for logs. To exit press CTRL+C
And, for example, to emit an error log message just type:
./emit_log_direct.js error "Run. Run. Or it will explode."
# => [x] Sent 'error':'Run. Run. Or it will explode.'
(Full source code for (emit_log_direct.js source) and (receive_logs_direct.js source))
Move on to tutorial 5 to find out how to listen for messages based on a pattern.