simple-amqp-manager
v1.0.3
Published
Light and easy to use configurator that utilizes 'amqplib'
Downloads
3
Readme
Introduction
Light and easy to use configurator that utilizes "amqplib"
Installation:
$ npm install simple_amqp_manager
Example
config.js
module.exports.config = {
protocol: 'amqp',
hostname: 'localhost',
port: 5672,
password: 'guest',
username: 'guest',
locale: 'en_US',
vhost: '/',
connectionName: 'Your Connnection Name',
connection: {
retries: {
enabled: true,
limit: 15,
delay: 5000,
}
}
}
amqpManager.js
const amqpManager = require("../amqpManager");
const { config } = require('../utils/config');
// Example server to test publishing
const server = http.createServer(async (req, res) => {
if (req.url === '/amqp/send') {
const message = new amqpManager.Message('messageType', {
messageContent: 'Test message content',
testProp: 'testValue',
time: new Date()
});
await amqpManager.publisher.publish(message);
res.writeHead(200);
console.log(message);
res.end(JSON.stringify(message));
}
});
// Establish connection with the broker
amqpManager.connectAmqp(config)
.then(async (connection) => {
console.log(`Connected to amqp broker on port ${connection.port}`);
await amqpManager.consumer.subscribe();
})
.catch((error) => console.log(`Failed to connected with error ${error.message}`));
amqpManager.publisher.on('messagePublished', (message) => {
console.log(`Published message [${message.options.correlationId}]`);
});
// Note "Consumer" is always concatenated to the end of "messageType" param
amqpManager.consumer.registerConsumer('messageType', (message, channel) => {
console.log('Received message: message.properties');
});