View on GitHub

Devendra's Log


If your HTML5 application requires RPC (remote procedure call) semantics, JSON-RPC is an easy specification to implement.

The implementation below allows sending requests and receiving responses, and leverages JQuery’s custom events to raise notifications (requests sent by server without an id). You’ll need to implement the send function, to send the RPC message to the server. A corresponding receive function should call dispatchMessage to handle a RPC message received from the server.

The following code snippet demonstrates how to handle a notification called foo, sent by a server

$(document).on("foo", function (e, request) {
    // do something with

The following code snippet demonstrates how to send a new request for a method called bar, and handle the corresponding response

// data is some object that will be stringified
var request = new Request("bar", data);
request.execute(function (response) {
    // do something with response.error or response.result

I’ll leave the server-side code to handle JSON-RPC as an exercise to the reader. I’ve intentionally modified the params attribute in JSON-RPC spec to data, because the former is a reserved keyword in C#. Using reserved keywords with dynamic programming capabilities of libraries such as JSON.NET causes compilation errors.