JSON-RPC over Golang Websocket03 Nov 2013
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
First let us look a jsonrpc sample in pure golang.
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 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:
- Samples of websocket package
- 基于Golang的Websocket Server
- Golang Sample of json/rpc over Websocket
- Access outside of Go to a JSON-RPC server
- Introducing WebSockets: Bringing Sockets to the Web
The complete example code is here: https://github.com/janzhou/GoWebRPC.