rpc-duplex

3.2.6 • Public • Published

rpc-duplex

Streamed RPC library for both Node.JS and the browser

NPM

Important notes

This implementation DOES NOT follow any generic RPC specification as far as I know. It intends to provide a stable stand-alone solution for remote-procedure-calling over websockets in both Node.JS and the browser.

Install

npm install --save rpc-duplex

Usage

Node.JS

const rpc = require('rpc-duplex');
 
// Creating a provider
const provider = rpc({}, {
  capitalize( str ) {
    return str.toUpperCase();
  },
  throwError( arg ) {
    throw new Error(arg);
  }
});
 
// Creating a consumer
const consumer = rpc();
 
// Connect consumer & provider
// Normally this goes through a network of sorts
provider.pipe(consumer).pipe(provider);
 
// Use provided functions
const remote = rpc.remote(consumer);
 
// Go async so you can copy-paste this code
(async () => {
  
  // Wait for functions to appear
  while(remote.capitalize) await new Promise(r=>setTimeout(r,100));
  
  // Call a remote function
  let result = await remote.capitalize('foobar');
  console.log(result); // FOOBAR
  
  // Errors are re-thrown
  try {
    await remote.throwError('hello world');    
  } catch(e) {
    console.log(e.message); // hello world
  }
  
})();

Browser

Browser usage is possible through the use of browserify (webpack should be supported but is not tested).

This package makes use of ES6 features. If you want to use this module in older browsers you'll need to use a plugin like esmify to ensure it works.

Package Sidebar

Install

npm i rpc-duplex

Weekly Downloads

8

Version

3.2.6

License

MIT

Unpacked Size

19.9 kB

Total Files

8

Last publish

Collaborators

  • finwo