Sobes.tech
Junior — Middle

RabbitMQ mesajlaşma sistemində iş təcrübəniz varmı?

sobes.tech Süni İntellekt

AI-dan cavab

Bəli, RabbitMQ ilə işləmə təcrübəm var. Bu, mesajların asinxron şəkildə mübadiləsini təmin edən mesaj brokeridir və Node.js-də amqplib kitabxanası ilə işləmək çox yaygındır.

Sadə bir mesaj göndərən və alan nümunəsi:

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);
    }
  });
}

// Mesaj göndərmək üçün sendMessage() çağırın, qəbul etmək üçün receiveMessage()