GithubHelp home page GithubHelp logo

claudijo / json-rpc-client Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 152 KB

JSON RPC 2.0 client implementation, which can be used with an arbitrary transport.

License: MIT License

JavaScript 100.00%

json-rpc-client's Introduction

json rpc client

A JSON-RPC 2.0 client implementation, which can be used with an arbitrary transport. For corresponding server implementation, see json-rpc-server.

The Client is defined as the origin of Request objects and the handler of Response objects.

Usage

Create request function with an arbitrary transport. The transport object MUST have a send method for sending string messages and it MUST have an on method for adding message event listeners.

Messages are batched if they are sent within the same event loop.

Example

// Channel "interface"
var channel = {
  // Sends the specified string over the wire.
  send: function(json) {
    // ...
  },

  // This method will be called with the string `message` as event and a
  // callback that expects the incoming message payload as a string.
  on: function(event, listener) {
    // ...
  },
};

var request = require('json-rpc-client')(channel);

// rpc call with positional parameters
// --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "generated-uuid"}
request('subtract', [42, 23], function(err, result) {
  // ...
});

//rpc call with named parameters
// --> {"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": "generated-uuid"}
request('subtract', {subtrahend: 23, minuend: 42}, function(err, result) {
  // ...
});

// a notification
// --> {"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}
request('update', [1, 2, 3, 4, 5]);

Test

Run unit tests:

$ npm test

Create test coverage report:

$ npm run-script test-cov

License

MIT

json-rpc-client's People

Contributors

claudijo avatar

Watchers

 avatar James Cloos avatar  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.