You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A library for making AMQP 0-9-1 clients for Node.JS, and an AMQP 0-9-1 client for Node.JS v10+. This library does not implement AMQP1.0 or AMQP0-10.
npm install amqplib
❤️ Help Support Jack
One of my close friends, Jack, was recently diagnosed with terminal brain cancer (grade 4 astrocytoma). He’s a young father facing an unimaginably tough road with remarkable courage. Thanks to chemotherapy, radiotherapy, and your support, two of his tumours have stopped showing activity. Donations help Jack continue accessing promising complementary therapies, attend hospital appointments, and spend meaningful time with his children.
If you’ve benefited from amqplib, please consider supporting Jack’s journey through his J Crushing Cancer gofundme page. Thank you - @cressie176
RabbitMQ Compatibility
Only 0.10.7 and later versions of this library are compatible with RabbitMQ 4.1.0 (and later releases).
Getting to 100% (or very close to 100%) test coverage
Callback API example
constamqplib=require('amqplib/callback_api');constqueue='tasks';amqplib.connect('amqp://localhost',(err,conn)=>{if(err)throwerr;// Listenerconn.createChannel((err,ch2)=>{if(err)throwerr;ch2.assertQueue(queue);ch2.consume(queue,(msg)=>{if(msg!==null){console.log(msg.content.toString());ch2.ack(msg);}else{console.log('Consumer cancelled by server');}});});// Senderconn.createChannel((err,ch1)=>{if(err)throwerr;ch1.assertQueue(queue);setInterval(()=>{ch1.sendToQueue(queue,Buffer.from('something to do'));},1000);});});
Promise/Async API example
constamqplib=require('amqplib');(async()=>{constqueue='tasks';constconn=awaitamqplib.connect('amqp://localhost');constch1=awaitconn.createChannel();awaitch1.assertQueue(queue);// Listenerch1.consume(queue,(msg)=>{if(msg!==null){console.log('Received:',msg.content.toString());ch1.ack(msg);}else{console.log('Consumer cancelled by server');}});// Senderconstch2=awaitconn.createChannel();setInterval(()=>{ch2.sendToQueue(queue,Buffer.from('something to do'));},1000);})();
Running tests
npm test
To run the tests RabbitMQ is required. Either install it with your package
manager, or use docker to run a RabbitMQ instance.
docker run -d --name amqp.test -p 5672:5672 rabbitmq
If prefer not to run RabbitMQ locally it is also possible to use a
instance of RabbitMQ hosted elsewhere. Use the URL environment
variable to configure a different amqp host to connect to. You may
also need to do this if docker is not on localhost; e.g., if it's
running in docker-machine.
One public host is dev.rabbitmq.com:
URL=amqp://dev.rabbitmq.com npm test
NB You may experience test failures due to timeouts if using the
dev.rabbitmq.com instance.
You can run it under different versions of Node.JS using nave:
nave use 10 npm test
or run the tests on all supported versions of Node.JS in one go:
make test-all-nodejs
(which also needs nave installed, of course).
Lastly, setting the environment variable LOG_ERRORS will cause the
tests to output error messages encountered, to the console; this is
really only useful for checking the kind and formatting of the errors.
LOG_ERRORS=true npm test
Test coverage
make coverage
open file://`pwd`/coverage/lcov-report/index.html