3.0.0 • Published 6 years ago

dead-simple-rpc v3.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

dead-simple-rpc

dead-simple-rpc is a minimalistic approach to asynchronously calling remote functions with Node.js.

Why? Originally inspired by dnode, I saw that a simpler and lighter solution could be made. And Promises are awesome.

Examples

Server:

const rpc = require( 'dead-simple-rpc' );

const server = rpc.Server( {
  handlers: {
    getMessage: ( args, callback ) => {
      callback( null, `Hi. Args: ${ JSON.stringify( args ) }` )
    }
  }
} );

server.listen()
.then( () => {
  console.log( 'listening...' );
} )
.catch( ( err ) => {
  console.error( 'an error occured: ', err );
} )
;

Client:

const rpc = require( 'dead-simple-rpc' );

const client = rpc.Client();

client.evoke( 'getMessage', { cake: 'isGood' } )
.then( ( response ) => {
  console.log( 'response: ', response );
} )
.catch( ( err ) => {
  console.error( 'an error occured: ', err );
} )
;

Options

Server defaults:

const options = {
  host: 'localhost',
  port: 7889,
  handlers: {}, // dictionary of functions
  debug: false // logging
};

const server = rpc.Server( options );

Client defaults:

// same as server, but without handlers
const client = rpc.Client( options );

Methods

Each method returns a promise

// --- All methods return a promise

// starts listening on the host/port specified in options
server.listen()

// closes the server
server.close()

// evokes `functionName` on the server with argument `args`; promise passes the response
// `args` can be any serializable object
client.evoke( functionName, args ) 

Response

 {
  timestamp_fn_start: 1522804238670, // when the function started
  timestamp_fn_end: 1522804239174, // when the function was resolved
  err: null, // the error message
  results: {
    cake: 'isGood'
  } // the result of the function
}

Testing

npm install -g tape
npm test