1.0.0 • Published 5 years ago

rmq-rpc v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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`);

    // Create connection and channel
    await mq.init();
    
    // Initialize an exchange and queue 'square'
    await mq.setupQueue('square');
    
    // Wait for requests
    mq.listenForMessages(async (request) => {
        // Handle 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`);

    // Create connection and channel
    await mq.init();

    // Send request and await result
    const result = await mq.sendRpc('square', num);

    console.log(`Num: ${num}, Square: ${result}`);