amqp.node.client
v1.1.5
Published
AMQP(Advanced Message Queue Protocol) Client of Node.js
Downloads
19
Readme
AMQP Client
An AMQP Client of Node.js base on amqplib with auto reconnect
Quick Start
Consumer
const AMQPClient = require('amqp.node.client');
const consumer = new AMQPClient({}, 1000);
const q = 'amqpclient.test';
consumer.start((ch) => {
return ch.assertQueue(q).then(ok => {
return ch.consume(q, (msg) => {
msg.should.be.Object();
if (msg) {
let body = msg.content.toString();
console.log(body);
ch.ack(msg);
}
});
});
});
Disconnect
$ rabbitmqctl close_all_connections test_reconnect_case
Producer
const AMQPClient = require('amqp.node.client');
const consumer = new AMQPClient();
const q = 'amqpclient.test';
const message = 'test message';
producer.start(ch => {
ch.assertQueue(q).then(ok => {
ch.sendToQueue(q, Buffer.from(message));
});
});
RPC
Server
/**
* @name rpcServer
* @param {function} srv must return Promise Object;
* @param {object} rpcOptions optional
*/
rpcServer.rpcService(opts=>{
const res = parseInt(opts.a)+parseInt(opts.b);
return Promise.resolve({val: res});
}, {queue: 'my.custom.rpc.request.queue.name'}).then(res=>{
console.log('rpc server ready');
});
Client
rpcClient.rpc({a:1, b:2}, {queue: q}).then(res=>{
res.val.should.be.equal(3);
});
Environment
RABBITMQ_HOST
default host if setRABBITMQ_VHOST
default vhost if setRABBITMQ_USER
default username if setRABBITMQ_PWD
default password if setRABBITMQ_PORT
default port if set