headers.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env node
  2. const amqp = require('../');
  3. (async () => {
  4. const connection = await amqp.connect();
  5. const channel = await connection.createChannel();
  6. process.once('SIGINT', async () => {
  7. await channel.close();
  8. await connection.close();
  9. });
  10. const { exchange } = await channel.assertExchange('matching exchange', 'headers');
  11. const { queue } = await channel.assertQueue();
  12. // When using a headers exchange, the headers to be matched go in
  13. // the binding arguments. The routing key is ignore, so best left
  14. // empty.
  15. // 'x-match' is 'all' or 'any', meaning "all fields must match" or
  16. // "at least one field must match", respectively. The values to be
  17. // matched go in subsequent fields.
  18. await channel.bindQueue(queue, exchange, '', {
  19. 'x-match': 'any',
  20. 'foo': 'bar',
  21. 'baz': 'boo'
  22. });
  23. await channel.consume(queue, (message) => {
  24. console.log(message.content.toString());
  25. }, { noAck: true });
  26. channel.publish(exchange, '', Buffer.from('hello'), { headers: { baz: 'boo' }});
  27. channel.publish(exchange, '', Buffer.from('hello'), { headers: { foo: 'bar' }});
  28. channel.publish(exchange, '', Buffer.from('lost'), { headers: { meh: 'nah' }});
  29. console.log(' [x] To exit press CTRL+C.');
  30. })();