npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-rabbit-rpc

v1.0.4

Published

Bluebird-based promise RPC using RabbitMQ

Downloads

20

Readme

rabbit-rpc

Bluebird-based promise RabbitMQ RPC using amqp.node

Install

> npm install promise-rabbit-rpc

example

Establish connection

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

Promises

handler.js

Lets write a handler, that will return the square of the number. Note that handler can accept any number of arguments. You can use ES6 spread feature. Function handler is fully-fledged element of promise chain. This means that you can throw an error, or return Promise.reject(), and caller instance will recieve it. Althoug you can return pure value or return Promise.resolve() with value - same result.

rpc.process('sum', function handler(...numbers) {
    if(numbers.length === 0) return Promise.reject(new Error('No numbers'));
    return Promise.resolve(numbers.reduce((a, sum) => sum + a));
})

Now we can call this handler from any instance. You can catch errors like if caller and handler are executed on one nodejs process. But actually they can be located in different parts of the globe.

rpc.promise('sum', 1, 2, 3)
    .then( res => console.log('The sum is ', res) )
    .catch( reason => console.error(reason) )

caller.js

If you don't want to use promise.

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

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) {
    let 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)
        });
    });
});