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

@nazar910/rpc-module

v1.2.3

Published

Module that provides rpc communication

Downloads

33

Readme

rpc-module Build Status

Module that provide you with a possibility to communicate with other modules of your system via rpc

Getting started

What is RPC?

RPC stands for "remote procedure call". Using such tool you can invoke function that is not part of your application. For example, you can make a call to another microservice if you're using such arhictecture.

Usage

First you need to install it into your project:

npm i -S @nazar910/rpc-module

Decide what communication transport you need (at the moment it is only AMQP, but I'm gonna implement other transports):

  • AMQP:
npm i -S amqplib

Now you can simply require and use it (see also AMQP example in the tests):

const rpcModule = require('@nazar910/rpc-module');
const { AMQPRPCClient, AMQPRPCServer } = rpcModule.getDriver('amqp');

const RABBITMQ_URI = 'amqp://localhost:5672';
async function main() {
    const server = AMQPRPCServer.create(RABBITMQ_URI);
    await server.start();
    server.addHandler('important', (job) => `"${job}" is very important`);

    const client = AMQPRPCClient.create(RABBITMQ_URI);
    const result = await client.call('important', 'doing something');
    console.log(result);
}

main();

API

  1. rpcModule.getDriver(driverName: string) Returns apropriate drivers (Server and Client) for specified driverName.
const rpcModule = require('@nazar910/rpc-module');
const { AMQPRPCServer, AMQPRPCClient } = rpcModule.getDriver('amqp');
  1. Drivers API

!Note: all driver have similar API for easy usage.

  • create(driverArgs)

AMQPRPCServer and AMQPRPCClient have create factory method for getting instance of driver (rpcServer or rpcClient). Example:

const rpcModule = require('@nazar910/rpc-module');
const { AMQPRPCServer, AMQPRPCClient } = rpcModule.getDriver('amqp');
const RABBITMQ_URI = 'amqp://localhost:5672';
const rpcServer = AMQPRPCServer.create(RABBITMQ_URI);
const rpcClient = AMQPRPCClient.create(RABBITMQ_URI);
  • start()

rpcServer and rpcClient have start method for ensuring connection. Required for server to start listening before registering handlers. Example:

// ...
async function () {
    await rpcServer.start();
    await rpcClient.start();
}
// ...
  • rpcServer.addHandler(command: string, job: Function)

rpcServer has addHandler method for registering handlers. Command is the name of rpc procedure and handler should be a function that returns Promise (or async function) and result will be sent to client. !!!Note: it is important to call rpcServer.start() before registering handlers. Example:

// ...
async function handler(...args) {
    console.log('Args are', args);
    return args;
}
async function () {
    await rpcServer.addHandler('foo', handler);
}
// ...
  • rpcClient.call

rpcClient has call(command: string, arg1, arg2, ..., argn) method for calling rpc. Command is the name of rpc procedure and args are arguments to be passed to rpc. Example:

// ...
async function () {
    const result = await rpcClient.call('foo', 'bar');
    console.log('Result is', result);
}
// ...
  • rpcClient.sendRaw

rpcClient has sendRaw(queueName: string, data: object) method for just sending msg to a queue. Example:

// ...
async function () {
    await rpcClient.sendRaw('some-queue', {foo: 'bar'});
}
// ...