send.js 847 B

12345678910111213141516171819202122232425262728293031
  1. #!/usr/bin/env node
  2. const amqp = require('amqplib');
  3. const queue = 'hello';
  4. const text = 'Hello World!';
  5. (async () => {
  6. let connection;
  7. try {
  8. connection = await amqp.connect('amqp://localhost');
  9. const channel = await connection.createChannel();
  10. await channel.assertQueue(queue, { durable: false });
  11. // NB: `sentToQueue` and `publish` both return a boolean
  12. // indicating whether it's OK to send again straight away, or
  13. // (when `false`) that you should wait for the event `'drain'`
  14. // to fire before writing again. We're just doing the one write,
  15. // so we'll ignore it.
  16. channel.sendToQueue(queue, Buffer.from(text));
  17. console.log(" [x] Sent '%s'", text);
  18. await channel.close();
  19. }
  20. catch (err) {
  21. console.warn(err);
  22. }
  23. finally {
  24. if (connection) await connection.close();
  25. };
  26. })();