The Beauty of RPC

Posted on July 4, 2010

As defined by Wikipedia:

In computer science, a remote procedure call (RPC) is an Inter-process communication that allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network) without the programmer explicitly coding the details for this remote interaction. That is, the programmer writes essentially the same code whether the subroutine is local to the executing program, or remote. When the software in question uses object-oriented principles, RPC is called remote invocation or remote method invocation.

I’ve recently worked on creating a controller class for Pylons that implements a form of RPC called JSON-RPC. It uses JSON (as you may have already determined) as the serialization format for initiating RPCs. And it turns out, this is really useful for web applications.

The beauty of it is that RPC allows us to translate from one language to another. As long as our source and target languages can both interpret the serialization format we are using to pass our method calls, we can expect that what we write in the source language will behave the same when we translate to the target language. For web applications this means that I can write my user interface code in Python and translate it later to Javascript. This is a really elegant way of building sophisticated user interfaces because it allows me to express my ideas using the semantics and idioms of user interfaces using the source language I am used to.

On the server-side I can write my controllers as services providing methods to a client application. This frees up my code from managing routing, headers, and serialization. All of those low-level details are handled by the JSONRPCController. I simply write normal-looking Python methods that are scarcely aware that they are serving a web-application at all.

So if you look at the entire stack from the top-down: Pyjamas, Pylons + JSONRPCController; the code doesn’t appear to be a web-application in the typical sense. The controller classes look more like DAOs. The interface code looks like typical desktop-interface code calling the DAOs to populate widgets and forms with data. This level of integration is possible because of JSON-RPC: when such an application is deployed, the Python interface code is compiled to Javascript and it works just as advertised.

As we port more and more of our desktop experience to the web, this will be the way to go for developers.