Remote scripting with SRSS

I have ever since I wrote my old article on remote scripting using the script tag been intrigued by the possibilities of “behind the scenes” communication. Then Harry Fuecks started writing about XMLHttpRequest and how to connect it with PHP in a seamless manner resulting in JPSpan. The result works much like CORBA (or like CORBA was supposed to work).

You as application developer shouldn’t be bothered about whether the object you work on is sitting on the client or the server. However good I found JPSpan a bit much for me as I normally just send pieces of data back and forth. Whence my own SSRS…

SSRS

I decided JPSpan was a bit much and started to hack up my own solution; SRRS (Project now dead and gone). Not a good name I know.

One thing I did like about JPSpan was the XML serialization that took place automatically. It makes it fairly easy to write a backend in most modern languages. As for me I mostly use PHP as the backend. Beginning with PHP5 there is finally good support for all the different XML technologies.

SSRS does not hide the remote classes. There are two different ways of calling remote scripts; post() and execute(). Both are synchronous and both are very similar. The latter, execute(), could be wrapped to handle execution of methods on remote classes whereas post() only send data back and forth. My main goal was that it should be simple to send and retrieve named variables from a backend so I’ll concentrate on the post() method here.

The client side

In javascript you would write for example

var result;
var props = new Array();
var obj;
props["login"] = "dotvoid";
props["fullname"] = "Danne Lundqvist";
props["age"] = 29;
try {
  obj = new RemoteObject('postHandler.php', 'User');
  result = obj.post(props);
  len = result.length();
  for (n = 0; n < len; n++) {
    alert(result.id(n) + " is " + result.item(n));
  }
}
catch(ex) {
  alert('Err: ' + ex.code);
}

Behind the scenes

This automatically would get serialized to the XML below and sent via a synchronous http POST call to postHandler.php. I’m not sure if it is a good idea to always encode strings in CDATA but it sure makes life simpler.

<class name="User">
<s id="login"><![CDATA[dotvoid]]></s>
<s id="fullname"><![CDATA[Danne Lundqvist]]></s>
<i id="age" v="29"/>
</class>

The backend

I haven’t created a nice backend just yet but reading this in PHP is very simple using, for example, DOM. The only thing I do before reading it is adding a DOCTYPE with the DTD. The DTD defines the id attributes to be of the type ID so that I can use getElementById() to retrieve elements from the XML. Btw, this does not seem to work in javascript for some reason. Very annoying!

$dtd = "<!DOCTYPE class SYSTEM \"postHandler.dtd\">";
$doc = new DOMDocument("1.0");
$doc->loadXML("$dtd\n$HTTP_RAW_POST_DATA");
$doc->validate();
$fullname = $doc->getElementById('fullname')->nodeValue;
$login = $doc->getElementById('login')->nodeValue;
$age = $doc->getElementById('age')->getAttribute('v');

Sending data back to the client should be done via a automatic serialization of data as well. But till that is in place it is very simple to send data back to the client. Below is a quite stupid – but simple – example.

header('Content-Type: text/xml; charset=UTF-8');
echo '<class name="User"><i id="result" v="0"/></class>';

Conclusion

It works and the codebase is very small. It suit my needs a lot better than hiding class method invocations which JPSpan does. Right now it is only synchronous and does not handle timeouts or errors grazefully. In addition there are a few things left before it fiels production worthy. So it will grow some more.

I have also started

Try it if you are so inclined and tell me if you think it could be useful or just a duplication of effort and as such a waste of time.

Javascript

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(required)

(required)