The JSON-RPC spec has been around for some time now (since 2005) though I have only just recently
become aware of it. The moment I saw It, I thought “brilliant!” just at the
site of its name. After looking into it
further I am surprised at how very simple it is… and it doesn't need to be more
complicated. It makes it quite easy to
write servers and clients in most languages.
This fits right there into one of the latest trends in software
engineering which is polyglot programming or using multiple languages in a
system to accomplish tasks using the best tool for the job. It also fits in to a distributed programming
model as well.
The JSON RPC specification doesn’t require a specific
transport mechanism; I implemented it over HTTP using scala as an example
implementation on both the client and server sides. One benefit of using HTTP is that you can use JavaScript to remotely call
a JSON-RPC methods. The
implementation could use other transports such as AMQP or many others.
The request
Requesting a remote invocation is easy; simply send a
message that looks like this:
{
“method”: “echo”,
“params”: [
“hello”
],
“id”: 1
}
While this is fairly self explanatory, it’s important to
note that if the “id” is set to null The
request becomes a notification which is specified to have no response. Otherwise, the “id” field should be used for
message coordination on the client side.
The “id” can be of any particular type that is valid in a JSON message.
The response
A response to the message posted above should look like:
{
“result”: “hello”,
“error”: null,
“id”: 1
}
The response is also quite self-explanatory. Every response message must contain the “id”
to the corresponding request message.
Additional Notes
A generic client should not make any assumptions about
message order when using a streaming connection. Responses may come back in a different order
than the requests that came in so use the “id” field to coordinate the messages.
Clients should pay attention to protocol specific errors to
handle them accordingly. If a client is
using the HTTP protocol there are a number of java/scala exceptions that may be
thrown in the process of establishing a connection which are outside of the “error”
field.
Clients and servers should validate the JSON messages and
handle errors accordingly.
Security must be implemented at several levels. The transport level will include encryption as well as authentication. As always, all input data should be validated
and checked to avoid injection, XSS and other known vulnerabilities. The frameworks you use may not always validate the data you will be dealing with.
Overall, I like the specification, it is simple and easy to use.
No comments:
Post a Comment