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

octopus-rpc

v2.8.0

Published

Super easy namespaced & bi-directional RPC over multiple transport types.

Downloads

7

Readme

octopus image

Octopus RPC

Super easy bi-directional RPCs, for Node JS & the browser, that just work !

  1. Handles an arbitrary mix of transports, including socket.io, Node forked (child) processes, websockets.
  2. Pluggable architecture, so you can easily add your own custom transports.
  3. RPC Calls are namespaced debug style ! ( ie local:child:* )
  4. Namespaces can be set dynamically from either end of the connection (calling or providing), at any time.

In use @ :

St8Flo

Install

Use below commmand for Node. Browserify can be used to bundle for the browser.

npm install octopus-rpc --save

Demo

See demo folder for an example - simple microservice style usage with node child process.

  1. Clone the repo
  2. Run demo/index.js using Node

Usage

1. Create a new RPC endpoint on each participating node

Each node should add itself under a unique namespace. The namespaces are dynamic, and can be changed from either side of the rpc (ie, calling or providing )

const octopus = require('octopus-rpc');
var rpc = octopus('local:parent:parent1');

2. Add RPC commands & providers.

Providers are optional. They are automatically set up across all transports, previously added to the RPC instance.

const octopus = require('octopus-rpc');
var rpc = octopus('local:parent:parent1');

var hello = rpc.command('hello');
hello.provide((data, prev, transportName)=> {
  // some action here
 });

3. Add transports & call RPCs with 'debug' like namespace filters !

Transports are a standard, direct connection (socket), between 2 participating entities. (for eg: client to server socket). Currently supported transports are socket.io, node forked (child) processes, websockets. Octopus expects a ready socket connection and does not handle connection/reconnections. That is left to the user to implement.

const octopus = require('octopus-rpc');
var rpc = octopus('local:parent:parent1');

var hello = rpc.command('hello');
hello.provide((data, prev, transportName)=> {
  // some action here
 });

const { fork } = require('child_process');
const child1 = fork('child1.js');
const child2 = fork('child2.js');

var tasks = [];
tasks.push(rpc.over(child1, 'processRemote'));
tasks.push(rpc.over(child2, 'processRemote'));

Promise.all(tasks)
.then(()=>{
  hello.call('local:*', 'aloha')
    .then((res)=>console.log(res));
});

Transport type | String identifier --- | --- child process | 'processRemote' Socket.io | 'socketio' Websocket | 'websocket'

Full example

Copied from the demo folder

index.js


const { fork } = require('child_process');
const octopus = require('octopus-rpc');
const child1 = fork('child1.js');
const child2 = fork('child2.js');

var rpc = octopus('local:parent:parent1');

var hello = rpc.command('hello');

hello.provide(function (data, prev, transportName) {
  return 'Parent :- Hey there ! ' + data.from;
});

var tasks = [];
tasks.push(rpc.over(child1, 'processRemote'));
tasks.push(rpc.over(child2, 'processRemote'));

Promise.all(tasks)
.then(()=>{

// Data passed to a call can be a value, or a function. If function, it is evaluated for every transport that matches the filter, and the return value of the function is used as data.

  hello.call('local:child:child1',{from:'Parent'})
    .then((resp) => console.log('\n\nGot "hello local:child:child1" response as :\n',resp));

  hello.call('local:child:child2',{from:'Parent'})
    .then((resp) => console.log('\n\nGot "hello local:child:child2" response as :\n',resp));

  hello.call('local:child:*',{from:'Parent'})
    .then((resp) => console.log('\n\nGot "hello local:child:*" response as :\n',resp));


});

child1.js

const octopus = require('octopus-rpc');
var rpc = octopus('local:child:child1');

rpc.over(process, 'processRemote');
var hello = rpc.command('hello');

hello.provide(function (data, prev, transportName) {
  return 'child1 :- Hey there ! ' + data.from;
});

child2.js

const octopus = require('octopus-rpc');
var rpc = octopus('local:child:child2');

rpc.over(process, 'processRemote');
var hello = rpc.command('hello');

hello.provide(function (data, prev, transportName) {
  return 'child2 :- Hey there ! ' + data.from;
});