AMQP-Messaging
Highly efficient library to communicate with other microservices using RabbitMQ(AMQP Protocol) in nodejs
We're using GZIP and MsgPack for serialization. We also do packing to have a high throughput in Rabbitmq.
Installing
Install via NPM:
$ npm install --save amqp-messaging
Passing RabbitMQ Address:
you need to set rabbitmq host address in the ENV variable as below:
AMQP_SERVER_ADDRESS= 'rabbitmq'
Creating messaging variable
const Messaging = require('amqp-messaging');
const messaging = new Messaging();
//now you can work with messaging APIs
Work Queues
When you want to push to a queue and go ahead, You're not waiting for any responses. you can see this example:
await messaging;await messaging;
default prefetchCount is set to 1. If you set it more than 1 Rabbitmq will give more than 1 items at once.
Publish/Subscribe
await messaging;await messaging;
RPC
await messaging; //In another microservice:const user = await messaging;console;
ttl
in addWorker is the time to live for the queue in RabbitMQ, messages will be removed from the queue if ttl
reached
ttl
in rpcCall option is how much should the caller wait for the response? Promise will be rejected if TTL reached
It's prefered to set both ttl
variables the same value.
Finish all jobs and stop all rabbitmq workers
You can stop Gracefully shutdown with command below. It will Pack and send All remaining items in the queue to Rabbitmq and cancels all Workers
const graceful = async { try await messaging; catch e console; await ; process;}; process;process;process;
Packing
This library is able to pack many messages into just one message and send a huge one to RabbitMQ Queue.
You need to set pack
option in the sendPush
method:
await messaging;await messaging;await messaging;
In this example, the library will not send messages Mehran
and Navid
immediately, it will wait until 3 seconds or 3 items to be packed together.
Packing is very helpful to reduce Rabbitmq's high CPU usage by lowering publish rates.
Develop
We're open for pull requests. in order to run tests just run npm run test