GithubHelp home page GithubHelp logo

tempbottle / node-msgpack-rpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bpot/node-msgpack-rpc

0.0 1.0 0.0 88 KB

node-msgpack-rpc is an implementation of the Msgpack-RPC protocol specification for node.js.

JavaScript 100.00%

node-msgpack-rpc's Introduction

node-msgpack-rpc

node-msgpack-rpc is an implementation of the Msgpack-RPC protocol specification for node.js. Msgpack-RPC is built ontop of the very fast MessagePack serialization format. This implementation supports tcp and unix socket transports (it may one day support UDP).

Simple Usage

The easiest way to create a server is with a handler object. All incoming calls will be invoked on the handler object:

var handler = {
  'add' : function(a, b, response) {
     response.result( a + b );
   }
}

var rpc = require('msgpack-rpc');
rpc.createServer();
rpc.setHandler(handler);
rpc.listen(8000);

a corresponding client might look like:

var c = rpc.createClient(8000, '127.0.0.1', function() {
  c.invoke('add', 5, 4, function(err, response) {
    assert.equal(9, response);
    c.close();
  }
});

Without a handler

rpc.createServer(function(rpc_stream) {
  rpc_stream.on('request', function(method, params, response) {
    if(method == 'add') {
      response.result( params[0] + params[1] );
    } else {
      response.error("unknown method!");
    }
  }

  rpc_stream.on('notify', function(method, params) {
    console.log("recieved notification: " + method);
  });
});
rpc.listen(8000);

Session Pool

This module also provides a session pool which allows you to re-use client connections:

var sp = new SesssionPool();
sp.getClient(8000, '127.0.0.1').invoke('hello','world', function(err, response) { ... });;
sp.getClient(8001, '127.0.0.1').invoke('hello','world', function(err, response) { ... });;

// Uses same tcp connection as above
sp.getClient(8000, '127.0.0.1').invoke('goodbye','world', function(err, response) { ... });;

sp.closeClients();

Installation

First you will need to install the node-msgpack add-on

To install node-msgpack-rpc with npm:

git clone http://github.com/bpot/node-msgpack-rpc/
cd node-msgpack-rpc
npm link .

RPC Stream API

Clients and the streams passed to servers for incoming connections are both instances of MsgpackRPCStream.

Methods

c.createClient(port, [hostname], [ready_cb]);
c.invoke(method, [param1, param2, ...], cb);
c.notify(method, [param1, param2, ...]);
c.setTimeout(milliseconds);  // Setting this will cause requests to fail with err "timeout" if they don't recieve a response for the specified period
c.close(); // Close the socket for this client
c.stream // underlying net.Stream object

Events

'ready' // emitted when we've connected to the server
'request' // recieved request
'notify' // recieved notification

TODO

  • UDP Support?

node-msgpack-rpc's People

Contributors

bpot avatar tomtaylor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.