amqplib
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
bitsyntax | 0.1.0 | 16.61 kB | MIT | prod | |
bluebird | 3.7.2 | 136.03 kB | MIT | prod | |
buffer-more-ints | 1.0.0 | 5.54 kB | MIT | prod | |
readable-stream | 1.1.14 | 20.15 kB | MIT | prod | |
safe-buffer | 5.2.1 | 9.74 kB | MIT | prod | |
url-parse | 1.5.10 | 16.61 kB | MIT | prod |
Amqplib is a library for creating AMQP 0-9-1 clients for Node.JS. It serves as an interface to interact with messaging platforms such as RabbitMQ. However, it's worth noting that amqplib does not implement AMQP 1.0 or AMQP 0-10. Its stability, API completeness, and extensive usage in production makes it an excellent choice for integrating AMQP-based messaging functionalities into Node.JS applications.
Amqplib can be integrated into a Node.JS application with the npm install amqplib
command. It follows either a callback or promise/async API structure, depending on your preference. Here are sample usage examples for both:
For Callback API:
const amqplib = require('amqplib/callback_api');
const queue = 'tasks';
amqplib.connect('amqp://localhost', (err, conn) => {
if (err) throw err;
// Listener
conn.createChannel((err, ch2) => {
if (err) throw err;
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');
}
});
});
// Sender
conn.createChannel((err, ch1) => {
if (err) throw err;
ch1.assertQueue(queue);
setInterval(() => {
ch1.sendToQueue(queue, Buffer.from('something to do'));
}, 1000);
});
});
For Promise/Async API:
const amqplib = require('amqplib');
(async () => {
const queue = 'tasks';
const conn = await amqplib.connect('amqp://localhost');
const ch1 = await conn.createChannel();
await ch1.assertQueue(queue);
// Listener
ch1.consume(queue, (msg) => {
if (msg !== null) {
console.log('Recieved:', msg.content.toString());
ch1.ack(msg);
} else {
console.log('Consumer cancelled by server');
}
});
// Sender
const ch2 = await conn.createChannel();
setInterval(() => {
ch2.sendToQueue(queue, Buffer.from('something to do'));
}, 1000);
})();
These examples demonstrate basic usage of the amqplib library to produce (send) and consume (receive) messages from a queue.
The amqplib documentation is hosted on GitHub Pages, and can be accessed at https://amqp-node.github.io/amqplib/. Here you'll find a comprehensive guide to the library's API reference, tips on troubleshooting, and a variety of examples derived from the RabbitMQ tutorials. Users can also view the change log to keep track of updates and changes made to the library over time. From initial setup to more complex tasks, the documentation provides all the information you need to integrate and leverage amqplib effectively in your Node.JS projects.