NodeJs — Message Queues with RabbitMQ
Message queues help microservices communicate reliably and asynchronously. RabbitMQ is a popular message broker for decoupling services and improving fault tolerance.
npm install amqplib
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'tasks';
const msg = 'Hello, RabbitMQ!';
await channel.assertQueue(queue);
channel.sendToQueue(queue, Buffer.from(msg));
console.log('Sent:', msg);
}
sendMessage();