Junior — Middle
Tell about your experience working with message brokers and their use in projects.
sobes.tech AI
Answer from AI
In projects with Node.js, I have worked with message brokers such as RabbitMQ and Apache Kafka. They are used to organize asynchronous message exchange between system components, which increases scalability and fault tolerance.
For example, in one project, RabbitMQ was used for processing background tasks: the web server sent messages to a queue, and a separate worker processed them. This allowed to offload the main thread and improve application responsiveness.
An example of sending a message to RabbitMQ in Node.js using the amqplib library:
const amqp = require('amqplib');
async function sendMessage() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const queue = 'tasks';
const msg = 'Process this task';
await channel.assertQueue(queue, { durable: true });
channel.sendToQueue(queue, Buffer.from(msg));
console.log("[x] Sent '%s'", msg);
setTimeout(() => {
connection.close();
}, 500);
}
sendMessage();
Thus, message brokers help build distributed and scalable systems with efficient data processing.