receive_logs.js 846 B

123456789101112131415161718192021222324252627282930
  1. #!/usr/bin/env node
  2. const amqp = require('amqplib');
  3. const exchange = 'logs';
  4. (async () => {
  5. try {
  6. const connection = await amqp.connect('amqp://localhost');
  7. const channel = await connection.createChannel();
  8. process.once('SIGINT', async () => {
  9. await channel.close();
  10. await connection.close();
  11. });
  12. await channel.assertExchange(exchange, 'fanout', { durable: false });
  13. const { queue } = await channel.assertQueue('', { exclusive: true });
  14. await channel.bindQueue(queue, exchange, '')
  15. await channel.consume(queue, (message) => {
  16. if (message) console.log(" [x] '%s'", message.content.toString());
  17. else console.warn(' [x] Consumer cancelled');
  18. }, { noAck: true });
  19. console.log(' [*] Waiting for logs. To exit press CTRL+C');
  20. } catch (err) {
  21. console.warn(err);
  22. }
  23. })();