Junior — Middle
Have you had experience working with the RabbitMQ messaging system?
sobes.tech AI
Answer from AI
Yes, I have experience working with RabbitMQ. It is a message broker that allows applications to exchange data asynchronously through queues. In Node.js, the amqplib library is often used to work with RabbitMQ.
Here is an example of a simple sender and receiver of messages:
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
const msg = 'Hello RabbitMQ!';
await channel.assertQueue(queue, { durable: true });
channel.sendToQueue(queue, Buffer.from(msg), { persistent: true });
console.log(" [x] Sent '%s'", msg);
setTimeout(() => {
connection.close();
process.exit(0);
}, 500);
}
async function receiveMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'task_queue';
await channel.assertQueue(queue, { durable: true });
channel.prefetch(1);
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", queue);
channel.consume(queue, (msg) => {
if (msg !== null) {
console.log(" [x] Received %s", msg.content.toString());
channel.ack(msg);
}
});
}
// Call sendMessage() to send, call receiveMessage() to receive