Giter Site home page Giter Site logo

jquery.jsonrpcclient.js's Introduction

JSON-RPC 2.0 Client for HTTP and WebSocket backends

This plugin requires jquery.json.js to be available, or at least the methods $.toJSON and $.parseJSON.

The plan is to make use of websockets if they are available, but work just as well with only http if not.

Usage example:

var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc' });
foo.call(
  'bar', [ 'A parameter', 'B parameter' ],
  function(result) { alert('Foo bar answered: ' + result.my_answer); },
  function(error)  { console.log('There was an error', error); }
);

Batch calls

In HTTP you can batch calls with the batch-method. You get a batch handler to make all call- and notify-requests on, and they will all be sent in a single request. When a WebSocket backend is available, the requests will be sent immediately.

Example:

var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc' });
foo.batch(
  function(batch) {
    batch.call('bar', [ 'A parameter', 'B parameter' ], success_cb1, error_cb1);
    batch.call('baz', { parameters: 'could be object' }, success_cb2, error_cb2);
  },
  function(all_result_array) { alert('All done.'); },
  function(error_data)       { alert('Error in batch response.'); }
);

Each result will be paired with it's own callback. The all_done_callback given first to batch is called when all other callbacks are done.

NB: When a WebSocket is available, the all_done_cb will be called as soon as all request are dispatched.

WebSocket

If a websocket backend is given, it will be used if the browser supports it:

var foo = new $.JsonRpcClient({ ajaxUrl: '/backend/jsonrpc', socketUrl: 'ws://example.com/' });
foo.call('bar', [ 'param' ], success_cb, error_cb);
--> websocket message: {"jsonrpc":"2.0","method":"bar","params":["param"],"id":3}

The http fallback will be used when the browser is not WebSocket capable, but NOT when the websocket fails to connect.

WebSocket other message handler

If a non-response message comes in, it can be forwarded to an external handler by giving the onmessage-option.

Using an already alive websocket - getSocket option

If you already have a websocket active and want that to be used for the JSON-RPC requests, you can use the getSocket option. getSocket should point to a function with the following interface:

@param onmessage_cb  getSocket will be called with an onmessage_cb that must be bound to the
                     onmessage event of the returned socket.

@return websocket|null  The returned object should act like a WebSocket: it must have the
                        property readyState, with a value of less than or equal to 1.  If less
                        than 1, it must have an onopen-property that can be set, and that will
                        be called when the socket is ready.  Also, it must be have the function
                        'call', taking a string.
                        It could also return null if no socket is available.

The main purpose of this is to couple the client with a matching server, that can take requests from the backend.

Test

The test-file is supposed to be run with JsTestDriver.

JSON-RPC 2.0

JSON-RPC 2.0 is a very simple protocol for remote procedure calls, agnostic of carrier (http, websocket, tcp, whatever…).

JSON-RPC 2.0 Specification

jquery.jsonrpcclient.js's People

Contributors

dervisevic avatar fiddur avatar

Watchers

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