Node.js Message Queue RabbitMQ
RabbitMQ is an open-source message broker and queue server built on the AMQP 0-9-1 protocol. It supports various message queue models, including simple mode, work queue, publish/subscribe, routing mode, and topic mode.
Installing RabbitMQ
Ensure the RabbitMQ server is installed. By default, RabbitMQ uses guest/guest as the username and password.
# Start RabbitMQ service
rabbitmq-server
# Access the management interface in a browser
http://localhost:15672/Installing Node.js Library
Use the amqplib library to interact with RabbitMQ.
npm install amqplibProducer
The producer sends messages to RabbitMQ.
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const queue = 'hello';
const msg = 'Hello World!';
ch.assertQueue(queue, { durable: false });
ch.sendToQueue(queue, Buffer.from(msg), { persistent: false });
console.log(" [x] Sent '%s'", msg);
});
setTimeout(() => { conn.close(); process.exit(0); }, 500);
});Consumer
The consumer receives and processes messages from RabbitMQ.
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const queue = 'hello';
ch.assertQueue(queue, { durable: false });
ch.consume(queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received '%s'", msg.content.toString());
}
}, { noAck: true });
});
});Work Queue
The work queue model allows multiple consumers to process tasks.
// Producer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const queue = 'tasks';
const msg = process.argv.slice(2).join(' ') || 'Hello World!';
ch.assertQueue(queue, { durable: false });
ch.sendToQueue(queue, Buffer.from(msg), { persistent: false });
console.log(" [x] Sent '%s'", msg);
});
setTimeout(() => { conn.close(); process.exit(0); }, 500);
});
// Consumer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const queue = 'tasks';
ch.assertQueue(queue, { durable: false });
ch.consume(queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received '%s'", msg.content.toString());
ch.ack(msg);
}
}, { noAck: false });
});
});Publish/Subscribe
The publish/subscribe model allows multiple consumers to subscribe to the same topic.
// Producer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'logs';
const msg = process.argv.slice(2).join(' ') || 'Hello World!';
ch.assertExchange(exchange, 'fanout', { durable: false });
ch.publish(exchange, '', Buffer.from(msg));
console.log(" [x] Sent '%s'", msg);
});
setTimeout(() => { conn.close(); process.exit(0); }, 500);
});
// Consumer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'logs';
ch.assertExchange(exchange, 'fanout', { durable: false });
ch.assertQueue('', { exclusive: true }, (err, q) => {
ch.bindQueue(q.queue, exchange, '');
ch.consume(q.queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received '%s'", msg.content.toString());
}
}, { noAck: true });
});
});
});Routing Mode
The routing mode allows messages to be sent to specific queues based on a routing key.
// Producer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'direct_logs';
const severity = (process.argv.slice(2)[0] || 'info').toUpperCase();
const msg = process.argv.slice(2).slice(1).join(' ') || 'Hello World!';
ch.assertExchange(exchange, 'direct', { durable: false });
ch.publish(exchange, severity, Buffer.from(msg));
console.log(" [x] Sent to '%s': '%s'", severity, msg);
});
setTimeout(() => { conn.close(); process.exit(0); }, 500);
});
// Consumer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'direct_logs';
const types = ['info', 'warning', 'error'];
ch.assertExchange(exchange, 'direct', { durable: false });
ch.assertQueue('', { exclusive: true }, (err, q) => {
types.forEach(type => {
ch.bindQueue(q.queue, exchange, type);
});
ch.consume(q.queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received '%s' from '%s'", msg.content.toString(), msg.fields.routingKey);
}
}, { noAck: true });
});
});
});Topic Mode
The topic mode supports more complex routing rules.
// Producer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'topic_logs';
const routingKey = (process.argv.slice(2)[0] || 'anonymous.info');
const msg = process.argv.slice(2).slice(1).join(' ') || 'Hello World!';
ch.assertExchange(exchange, 'topic', { durable: false });
ch.publish(exchange, routingKey, Buffer.from(msg));
console.log(" [x] Sent to '%s': '%s'", routingKey, msg);
});
setTimeout(() => { conn.close(); process.exit(0); }, 500);
});
// Consumer
const amqp = require('amqplib/callback_api');
amqp.connect('amqp://localhost', (err, conn) => {
conn.createChannel((err, ch) => {
const exchange = 'topic_logs';
const bindings = [
'kern.*',
'*.critical',
'kern.critical'
];
ch.assertExchange(exchange, 'topic', { durable: false });
ch.assertQueue('', { exclusive: true }, (err, q) => {
bindings.forEach(binding => {
ch.bindQueue(q.queue, exchange, binding);
});
ch.consume(q.queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received '%s' from '%s'", msg.content.toString(), msg.fields.routingKey);
}
}, { noAck: true });
});
});
});



