AmqpCacoon
Caution
WIP: Documetation is very much a work in progress.
Overview
This is a basic library to provide amqp support. This library is a wrapper around amqplib and makes amqp easier to work with.
Features
- Simple interace around amqplib
- Publish flow control included out of the box (Wait for drain event if we can't publish)
- TODO timeout if drain event does not occure after some amount of time
Requirements to tests
- docker
- docker-compose
If using macos brew cask install docker
should install both docker and docker-compose
Running Tests
- Start docker-compose - This starts RabbitMq
docker-compose up -d
- Install node modules (This also loads local modules from our own repositories)
npm install
- Run tests
npm run test
Usage
Connect and publish
const config = {
messageBus: {
protocol: 'amqp',
username: 'valtech',
password: 'iscool',
host: 'localhost',
port: 5672,
testQueue: 'test-queue',
},
};
let amqpCacoon = new AmqpCacoon({
protocol: config.messageBus.protocol,
username: config.messageBus.username,
password: config.messageBus.password,
host: config.messageBus.host,
port: config.messageBus.port,
amqp_opts: {},
providers: {
logger: logger,
},
});
let channel = await amqpCacoon.getPublishChannel();
channel.assertQueue(config.messageBus.testQueue);
await amqpCacoon.publish(
'',
config.messageBus.testQueue,
Buffer.from('TestMessage')
);
Connect and Consume
const config = {
messageBus: {
protocol: 'amqp',
username: 'valtech',
password: 'iscool',
host: 'localhost',
port: 5672,
testQueue: 'test-queue',
},
};
let amqpCacoon = new AmqpCacoon({
protocol: config.messageBus.protocol,
username: config.messageBus.username,
password: config.messageBus.password,
host: config.messageBus.host,
port: config.messageBus.port,
amqp_opts: {},
providers: {
logger: logger,
},
});
let channel = await amqpCacoon.getConsumerChannel();
channel.assertQueue(config.messageBus.testQueue);
amqpCacoon.registerConsumer(
config.messageBus.testQueue,
async (channel: Channel, msg: ConsumeMessage) => {
console.log("Messsage content", msg.content.toString());
}
);