/rabbit-rpc

Simple RPC using RabbitMQ

Primary LanguageJavaScript

rabbit-rpc

simple rpc using RabbitMQ and amqp.node

Install

> npm install rabbit-rpc

example

caller.js

var rpc = require('rabbit-rpc')('amqp://localhost');

rpc.call('square', 99, function (err, res) {
    console.log('The square of 99 is', res);
});

handler.js

var rpc = require('rabbit-rpc')('amqp://localhost');

rpc.handle('square', function (num, callback) {
    callback(null, num*num);
});

Promises

rpc.promise('square', 99).then(function (res) {
    console.log('The square of 99 is', res);
});

Multiple arguments

rpc.call('sum', 1, 2, 3, function (err, res) {
    console.log('The sum is', res);
});

rpc.handle('sum', function (a, b, c, callback) {
    callback(null, a + b + c);
});

Multiple types

Send strings, numbers, arrays, objects or buffers. Arguments are serialized to BSON using node-buffalo.

rpc.call('getFile', __dirname, 'getfile.js', function (err, stats, data) {
    if (err) {
        return console.error('Got error', err);
    } else {
        console.log('Got file', stats.size, data.length);
    }
});

rpc.handle('getFile', function (dir, filename, callback) {
    var path = dir + '/' + filename;
    fs.stat(path, function (err, stats) {
        if (err) return callback(err);
        fs.readFile(path, function (err, data) {
            if (err) return callback(err);
            callback(null, stats, data)
        });
    });
});