RabbitMQ tutorial - "Hello World!" — RabbitMQ

  1. node receiver.js
  2. node receiver.js
  3. node sender.js

다수의 receiver가 존재함에도 단일 메세지는 동시에 전달되지 않는다.

먼저 등록된 receiver부터 차례대로 교차 전달된다. (Round-robin 방식)

#!/usr/bin/env node

var amqp = require("amqplib/callback_api");

amqp.connect(
  "amqp://myuser:secret@localhost:5672",
  function (error0, connection) {
    if (error0) {
      throw error0;
    }
    connection.createChannel(function (error1, channel) {
      if (error1) {
        throw error1;
      }

      var queue = "hello";

      channel.assertQueue(queue, {
        durable: false,
      });

      console.log(
        " [*] Waiting for messages in %s. To exit press CTRL+C",
        queue
      );

      channel.consume(
        queue,
        function (msg) {
          console.log(" [x] Received %s", msg.content.toString());
        },
        {
          noAck: true,
        }
      );
    });
  }
);
#!/usr/bin/env node

var amqp = require("amqplib/callback_api");

amqp.connect(
  "amqp://myuser:secret@localhost:5672",
  function (error0, connection) {
    if (error0) {
      throw error0;
    }
    connection.createChannel(function (error1, channel) {
      if (error1) {
        throw error1;
      }

      var queue = "hello";
      var msg = "Hello World!";

      channel.assertQueue(queue, {
        durable: false,
      });
      channel.sendToQueue(queue, Buffer.from(msg));

      console.log(" [x] Sent %s", msg);
    });
    setTimeout(function () {
      connection.close();
      process.exit(0);
    }, 500);
  }
);