RabbitMQ tutorial - Routing — RabbitMQ

Untitled

Direct exchange

Routing through direct exchange

Routing through direct exchange

Multiple bindings: It is perfectly legal to bind multiple queues with the same binding key.

Multiple bindings: It is perfectly legal to bind multiple queues with the same binding key.

#!/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 exchange = 'direct_logs';
        var args = process.argv.slice(2);
        var msg = args.slice(1).join(' ') || 'Hello World!';
        var severity = (args.length > 0) ? args[0] : 'info';
        
        channel.assertExchange(exchange, 'direct', {
          durable: false,
        });
        channel.publish(exchange, severity, Buffer.from(msg));
        console.log(' [x] Sent %s: \\'%s\\'', severity, msg);
      });
      
      setTimeout(function() {
        connection.close();
        process.exit(0);
      }, 500);
    });
#!/usr/bin/env node

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

var args = process.argv.slice(2);

if (args.length == 0) {
  console.log('Usage: receive_logs_direct.js [info] [warning] [error]');
  process.exit(1);
}

amqp.connect('amqp://myuser:secret@localhost:5672',
    function(error0, connection) {
      if (error0) {
        throw error0;
      }
      connection.createChannel(function(error1, channel) {
        if (error1) {
          throw error1;
        }
        var exchange = 'direct_logs';
        
        channel.assertExchange(exchange, 'direct', {
          durable: false,
        });
        
        channel.assertQueue('', {
          exclusive: true,
        }, function(error2, q) {
          if (error2) {
            throw error2;
          }
          console.log(' [*] Waiting for logs. To exit press CTRL+C');
          
          args.forEach(function(severity) {
            channel.bindQueue(q.queue, exchange, severity);
          });
          
          channel.consume(q.queue, function(msg) {
            console.log(' [x] %s: \\'%s\\'', msg.fields.routingKey,
                msg.content.toString());
          }, {
            noAck: true,
          });
        });
      });
    });

Untitled