itp-amqp-node
v1.1.0
Published
A lightweight AMQP client for Node.js.
Downloads
1
Readme
ITP AMPQ Node
A lightweight AMQP client for Node.js
Installation
npm install itp-amqp-node
Example
Producer
const AMQPProducer = require('itp-amqp-node').AMQPProducer;
const producer = new AMQPProducer({
exchange: 'itp.demo',
exchange_type: 'topic',
amqp_uri: process.env.RABBITMQ_URI || 'amqp://localhost'
});
producer.start()
.then(() => {
console.log('Connected to producer');
}, (err) => {
console.error('Error while connecting to producer', err);
});
try {
const routing_key = 'key.hello_world';
const message = JSON.stringify('Hello World');
producer.publish(routing_key, new Buffer(message));
} catch (err) {
console.error('Error while connecting to producer', err);
}
Consumer
const AMQPConsumer = require('itp-amqp-node').AMQPConsumer;
class ConsoleConsumer extends AMQPConsumer {
handleMessage (msg, resolve, reject) {
try {
const jsonMsg = msg.content.toString('utf-8');
console.log(JSON.parse(jsonMsg));
resolve();
} catch (e) {
console.error(e);
reject(e);
}
}
}
const opts = {
exchange : 'itp.demo',
exchange_type : 'topic',
amqp_uri : process.env.RABBITMQ_URI || 'amqp://localhost',
routing_key : 'key.hello_world'
};
const ConsoleConsumer = new ConsoleConsumer(opts);
consoleWorker.start();
License
ITP-AMQP-Node is freely distributable under the terms of the MIT license.