AndreGarzia.Com all we do is code

Rockets RPC Demo

Introducing Rockets RPC


What is Rockets RPC?


It is a simple library that makes it to create web services. It allows you to share revolution handlers with web-savvy application. Most web application involves calling a server side component and using the output from that call somewhere, instead of reinventing the wheel, RocketsRPC provides an easy way to share handlers and does all the output piping for you.

Add Two Numbers Example

Suppose you have the following handler which you want to share:

on addTwoNumbers pNumber1, pNumber2
return (pNumber1 + pNumber2)
end addTwoNumbers

With RocketsRPC you can expose this handler so that a web page or network aware application can call your code directly by accessing a url like:

http://andregarzia.com/cgi-bin/addTwoDemos.cgi?func=addTwoNumbers&pNumber1=2&pNumber2=2

if you try that url, you'll see it outputs the result from calling addTwoNumbers with pNumber1 = 2 and pNumber2 = 2. To see a live demo of this code, go to http://andregarzia.com/addTwoDemos.html , try it for I'll explain it piece by piece below.

The addTwoDemos.cgi




addTwoDemos.cgi

The web page


FirefoxSnapz004

http://andregarzia.com/addTwoDemos.html


The web page source


FirefoxSnapz005

You can see there's a minimal javascript to call 'addTwoNumbers' with the form parameters and glue the result into the 'span' tag. You may be thinking, how does javascript can call 'addTwoNumbers' like that, where is all the xmlHttpRequest part?

Well, RocketsRPC will generate javascript libraries for your CGI at runtime. If you call your CGI passing a param "func" with value "jslib", RocketsRPC will return a javascript library with proxy functions with the same name and parameters as the functions you exposed in your code. When you call those proxy functions, they go to the server, call the correct handler and return the result.

If you want to see the javascript library generated for AddTwoNumber.cgi just browse to http://andregarzia.com/cgi-bin/addTwoDemos.cgi?func=jslib

FirefoxSnapz006

We can simply include the runtime generated library using a script tag with a source value such as we did on the web page source.

FirefoxSnapz005.jpg

So, in the end, it is just a matter of exposing functions with the 'addFunction' call, including the javascript library in the webpage and then calling the functions as they were javascript native functions.

Can it get any easier?

Andre
|