1.0.27 • Published 4 years ago
rabbitmq-events-client v1.0.27
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:
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
});
1.0.19
4 years ago
1.0.18
4 years ago
1.0.17
4 years ago
1.0.16
4 years ago
1.0.22
4 years ago
1.0.21
4 years ago
1.0.20
4 years ago
1.0.26
4 years ago
1.0.25
4 years ago
1.0.24
4 years ago
1.0.23
4 years ago
1.0.27
4 years ago
1.0.15
4 years ago
1.0.14
4 years ago
1.0.13
4 years ago
1.0.12
4 years ago
1.0.11
4 years ago
1.0.9
4 years ago
1.0.10
4 years ago
1.0.8
4 years ago
1.0.7
4 years ago
1.0.6
4 years ago
1.0.5
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.4
4 years ago
1.0.3
4 years ago
1.0.0
4 years ago