An AMQP Client of Node.js base on amqplib with auto reconnect
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);
}
});
});
});
$ rabbitmqctl close_all_connections test_reconnect_case
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));
});
});
/**
* @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');
});
rpcClient.rpc({a:1, b:2}, {queue: q}).then(res=>{
res.val.should.be.equal(3);
});
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