rabbitmq-events-client
v1.0.27
Published
- This implementation is for using [rabbitmq](https://www.rabbitmq.com/) with events following [event-driven architecture](https://en.wikipedia.org/wiki/Event-driven_architecture#:~:text=Event%2Ddriven%20architecture%20(EDA),sale%22%20to%20%22sold%22.).
Downloads
5
Readme
RabbitMQ Client Events
Notes
- This implementation is for using rabbitmq with events following event-driven architecture. The library is mainly configured for topic exchanges, but could be changed (this is the most optimal ways for EDA).
- rabbitmq topic exchange example:
Route Key
- For creating routes keys is used https://github.com/fmvilas/topic-definition, formatted as follows:
Installation
npm i rabbitmq-events-client
Implementation
Publisher
import RabbitmqClientEvents from "rabbitmq-events-client";
const rabbitmqClientPublisher = new RabbitmqClientEvents("amqp://rabbitmq:rabbitmq@localhost", 'service name', 'exchange name', 'company name');
rabbitmqClientPublisher.connectToServer()
.then(() => {
setInterval(async () => {
await rabbitmqClientPublisher.publish(event, {
data: 123,
meta: {},
}
}, interval);
})
.catch((e) => {
console.error(e);
});
publish
function will create route key as mentioned above
Consumer / Worker
import RabbitmqClientEvents from "rabbitmq-events-client";
const rabbitmqClientConsumer = new RabbitmqClientEvents("amqp://rabbitmq:rabbitmq@localhost", 'service name', 'exchange name', 'company name');
rabbitmqClientConsumer
.connectToServer()
.then(async () => {
await rabbitmqClientConsumer.subscribe('queue name', 'key', (msg) => {
const content = JSON.parse(msg.content.toString());
//do things!!
//then acknowledge message
rabbitmqClientConsumer.ackMessage(msg);
//or reject message
rabbitmqClientConsumer.nackMessage(msg);
});
})
.catch((e) => {
console.error(e);
});
Modify Route Key
its posible alter the route key before publish a message, example:
...
rabbitmqClientEvents.setPublishKey({
company: 'company',
service: 'service',
version: 1,
entity: 'entity',
});
await rabbitmqClientPublisher.publish...
Dead Letter Exchange
By default will create a dead letter exchange and a queue,
it's possible to turn off this option in the subscribe
function:
rabbitmqClientConsumer.subscribe('queue name', 'key', (msg) => {
const content = JSON.parse(msg.content.toString());
//do things!!
rabbitmqClientConsumer.ackMessage(msg);
},{
createDeadLetterQueue: false
});