RMQ-RPC
Simple, lightweight RPC calls with RabbitMQ and Node.js based on amqplib.
The RmqRpc class features an easy API for the producer and consumer of the RPC call.
Installation and quickstart
npm install rmq-rpc
Require the module:
const RmqRpc = require('rmq-rpc');
Implement the consumer (server):
const mq = new RmqRpc(`amqp://user:password@host`);
await mq.init();
await mq.setupQueue('square');
mq.listenForMessages(async (request) => {
return Math.pow(Number(request), 2);
}).then(() => {
console.log('Server ready.')
});
Implement the producer (client):
const num = Math.random();
const mq = new RmqRpc(`amqp://${user}:${password}@localhost`);
await mq.init();
const result = await mq.sendRpc('square', num);
console.log(`Num: ${num}, Square: ${result}`);