Writing my own JSON-RPC server in NodeJS

So summer is over (I know, like it matters for working people… but it does) and all of my friends who were interning in NYC are gone. Sounds pretty depressing but the good thing about it is that now I got some time to work on my side projects. 1 of those is my implementation of the JSON-RPC protocol in NodeJS. The source code can be found https://github.com/longlho/node-jsonrpc. Therefore, this blog post would sorta serve the purpose of what I found out while writing this module (It’s not finished yet!!!). Oh the JSON-RPC spec can be found here: http://groups.google.com/group/json-rpc/web/json-rpc-2-0

So my goals for writing this module is to sorta create a framework for MetaDB, another project my friend and I have been working on. Thus, although I tried to keep it as generic as I can, there’re some design patterns that utilizes my server and allows it to (in the future) grab metadata from service classes themselves and do some magic (whether it’s better error feedback or introspection or authentication, I’m still working on it). I also found quite a few other implementations out there, including 1 in Connect but it doesn’t seem to do some of the stuff I want it to do.

For example, I want my server to have to ability to automatically register all public methods in a module, which allows namespacing when I write my service handlers. The other thing is Introspection, which proves to be pretty challenging. Streaming and authentication would also be on my list and those are a bit tricky to implement without bloating up the API.

Once this server is complete, ideally I would try to fire up 2 instances, 1 on my VPS, 1 on my friend’s VPS and grab a reserve-proxy/load-balancer to test run this thing, hopefully with distributed MongoDB/CouchDB as well.

So why JSON-RPC but not RESTful? I personally find  JSON-RPC more convenient for programmers as the design was to call a method (Procedure Call) remotely, thus the name RPC. For me personally it’s more intuitive than sending GET/POST/PUT/DELETE. JSON-RPC servers also serves 1 entry point instead of RESTful multiple URLs which IMO makes the driver layer a little bit easier to manage. The other reason was that it’s message type is JSON and NodeJS is in JavaScript. That actually got rid of a lot of serialize/deserialize/type conversion code.

One of the challenging things about JSON-RPC and NodeJS (or at least challenging for me coming from a heavy static-typing Java background) is that everything is in callback and it’s dynamic typing. For example, here’s how I get a response body in Java:

response.getBodyAsString();

And here’s how I get response body in NodeJS:

var resString = "";
response.on('data', function(chunk) {
   resString += chunk;
});
response.on('end', function() {
   console.log(resString);
});

The dynamic typing part is actually very flexible when ur writing a service, but when it comes to generating documentation for Introspection. Again, I’m spoiled since Java has Annotation and static-typing, but when I first started using jQuery, for example, it was pretty hard to figure out what the format of a parameter is. They’ve improved that a lot but the point is it might require a lot of manual efforts instead of auto-generate one.

Anyway this project has helped me a lot in learning NodeJS and JS in general. Some of the stuff below might be useful is ur doing this:

http://www.yuiblog.com/blog/2010/08/30/yui-theater-douglas-crockford-crockford-on-javascript-scene-6-loopage-52-min/

http://www.olympum.com/future/nodejs-to-v8-or-not-to-v8/

 

 

 

 

Advertisements

One thought on “Writing my own JSON-RPC server in NodeJS

  1. beach body products…

    […]Writing my own JSON-RPC server in NodeJS « LLH…azndezign[…]…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: