JSON-RPC over Golang Websocket

Basic ideas

JSON-RPC is a lightweight remote procedure call protocol. The request of JSON-RPC is a single object serialized using JSON, a lightweight data-interchange format most commonly used in web applications to send data from the server to the browser. Typically JSON data is transfered using Ajax. But WebSocket represents the next evolutionary step in web communication. It support two way communication, provide bi-directional, full-duplex communications channels, over a single TCP socket. So that we can implement jsonrpc in a bi-directional way.

Golang jsonrpc server & client

Golang‘s jsonrpc is an implementation of JSON-RPC protocol. Here is an example for the jsonrpc package.

First let us look a jsonrpc sample in pure golang.

Jquery-jsonrpc

jquery-jsonrpc is a JSON RPC 2.0 compatible client library and jQuery (1.4, 1.5, and 1.6 compatible) plugin. But it not implemented the jsonrpc server. I write a jsonrpc library which implemented both the server and client when a websocket was created.

Golang Websocket

Golang has a third party websocket implementation that we can use. You need to get the websocket package first:

go get code.google.com/p/go.net/websocket

Then you can try these examples:

The complete example code is here: https://github.com/janzhou/GoWebRPC.