Wanna let your software to series of jobs? You don't want to use extensive and complicated complex queueing system. Easy RabbitMQ provide simple and easy use of RabbitMQ for you.
Project site: [https://github.com/huferry/easy-rmq]
First, install the package:
npm i -s easy-rmq
Connect to the queue as simple as:
const conn = awaitconst queue = await conn
and publish your message just like this:
or if you're from the consuming side:
But, if you're outside a non-async function:
- Connecting to Server
- Access to The Queue
- Publish A Message
- Subscribing for Messages 4.1. Handler Function 4.2. Error Handling
1. Connecting to Server
The module is exporting a single async function
connect that will take an object containing properties to connect to the amqp server.
Note that this function is asynchronous and will return a Promise object that returns one function
queue to access the queue.
2. Access to The Queue
Accessing to the queue can be made by the the
Note that you don't have to create the queue on the server. We are basically doing a code-first queueing process. In the example above, the queue 'my-queue' will be created automatically on the server.
3. Publish A Message
3.1. Simple Message
publish function takes the
payload of the queue message and this can be any JSON serializable object. The payload will be carried onto the queue message and delivered by the subscribing function.
3.2. Propagate to Multiple Messages
payload argument corresponds with a single object. In case the
payload is an array then the it will be split into multiple messages and every single object in the array will be a
payload on its own, resulting in multiple queue entries.
4. Subscribe for Messages
The last part of this module is to subscribe for the queue messages (and then do some processing). From the
queue object, use the
subscribe function. This will take in as arguments the
handler function and the
handler function is given the
payload object as argument (see example).
4.2. Error Handling
Any error thrown in the processing will triggers the error handler (see example in previous section). The error handling function, which is provided by the user, will be given 2 arguments: the
error and a
requeue function. In case that the user decided that he wants to retry the processing then he can invoke the
requeue function. The user can set a delay in milliseconds to this function. If no delay is provided it will default to 1 seconds.