RabbitMQ
RabbitMQ - an AMQP client for NodeJS
Installation
"dependencies":
npm install amqp-library
Requirements
- set the rabbitmq url in your .env.
RABBITMQ_URL=amqp://localhost
or
//when initializing RabbitMQ, pass in the urlrabbitMQ;
Initialization
const RabbitMQ = ;
Basic functionality
- Create A Channel
//(async/await)const rabbitMQ = ;const connection = await rabbitMQ;const channel = await rabbitMQ;
- To queue a data for processing
//(async/await)const rabbitMQ = ;const connection = await rabbitMQ;const channel = await rabbitMQ; const payload = timestamp: Date name: "A Name" email: "Email" ;const data = await rabbitMQ;console; //{data: true}
-- To assert/create an exchange
const exchangeName = "logs";const rabbitMQ = ;const connection = await rabbitMQ;const exchange = await rabbitMQ; //exchange types includes fanout, direct, topic and header.checkout https://www.rabbitmq.com for more exchange types. console;//Exchange { exchange: { exchange: 'test-exchange' } }
-- To Publish to an exchange
const exchangeName = "logs";const rabbitMQ = ;const connection = await rabbitMQ;const exchange = await rabbitMQ;const push = await rabbitMQ; console;//{ push: true }
-- To create/assert A queue
const exchangeName = "logs";const queueName = "test-exchange-queue";const queueOption = exclusive: true;const bindKey = "route"; //read more on routing here https://rabbitmq.com/tutorials/tutorial-four-javascript.htmlconst rabbitMQ = ;const connection = await rabbitMQ;const queue = await rabbitMQ;console;// queue: { queue: 'test-exchange-queue', messageCount: 0, consumerCount: 0 }
-- To listen to a queue and pull data for processing
//(async/await)const rabbitMQ = ;const connection = await rabbitMQ;const channel = await rabbitMQ; rabbitMQ; //Warning: If you enable acknowledgement {noAck: false}, the next data on the queue //won't be released by the queue until the current data is acknowledge.
-- To close a connection
rabbitMQ;
Tests
Cli
npm installnpm test