Ivan Zuzak home about blog projects talks & papers other

RPC for Web Workers and distributed computing within the browser

21 Dec 2009

In a previous post I wrote about pmrpc - a project of mine for developing a JavaScript library for cross-domain inter-window RPC-style communication in HTML5 browsers. RPC communication is based on two technologies: HTML5 cross-domain messaging as the transport mechanism and JSON-RPC as the RPC protocol and message format, while the library supports other advanced features (access control, asynchronous calls, retries...). At the end of the post I outlined a few things Marko and I wanted to implement next in pmrpc, one of which was support for Web Workers. From the post:

Web Workers are another specification being developed in parallel with HTML5. The specification “defines an API for running scripts in the background independently of any user interface scripts”. What’s interesting is that the postMessage API is used to communicate with web workers and therefor pmrpc could be extended to include support for web workers.

Well, the API used to send messages from a web page to a worker and vice versa is not exactly the same as the cross-document messaging API, but is very similar. So, the idea for supporting Web Workers was leave the pmrpc API for registering, calling and unregistering procedure unchanged, and just extend it to also use the Web Worker API for message transport (in addition to the cross-doc messaging). And about two weeks ago we released a new version of pmrpc with Web Worker support. Here's an example of using pmrpc with Web Workers:

 1 // [worker object A - testingWorker.js]
 2 
 3 // load pmrpc library
 4 importScripts('pmrpc.js');
 5 
 6 // expose a procedure
 7 pmrpc.register( {
 8 publicProcedureName : "HelloPMRPC",
 9 procedure : function(printParam) {
10               alert(printParam); } } );
 1 // [window object B]
 2 
 3 // load pmrpc library
 4 <script type="text/javascript" src="pmrpc.js"></script>
 5 
 6 // create worker
 7 <script type="text/javascript">
 8   var testingWorker =
 9     new Worker("testingWorker.js");
10 
11   // calls the exposed procedure
12   pmrpc.call( {
13   destination : testingWorker,
14   publicProcedureName : "HelloPMRPC",
15   params : ["Hello World!"] } );
16 </script>

A few notes on worker support. Pmrpc currently supports only normal ("dedicated") Web Workers. Shared Workers (workers shared between multiple web pages) and nested workers (workers within workers) are currently not supported since, as far as I know, Shared and nested Workers are not implemented yet in any browser. Also, since Workers must be loaded from the same origin as the page that created them - the access control feature in pmrpc make no sense and are therefore not supported for Web Workers.

Next on our list is enabling discovery of registered procedures scattered over multiple browser workers, iframes and windows. Currently, pmrpc requires that you tell it where the procedure you are calling is, but sometimes you don't know where it is, or don't care - e.g. you just want to call the "DrawMap" procedure loaded from domain "http://www.good.com" if it exists. Discovery should enable discovery of methods based just on their names. The problems in implementing this feature (knowing which windows, iframes and workers are currently running and syncing the list of registered procedures with each of them) made me think about where systems running in browsers are heading - towards a form of distributed & parallel computing, only within a single browser. Distributed and concurrent execution, discovery, access control, synchronization, remote invocation, fault tolerance and naming are all concepts well know in distributed computing, and will possibly be re-invented in the context of web browsers to support this new shift. I'm not sure how far pmrpc will go in this direction, but I hope it will be a good example for other developers and research projects.

Do you think that web browsers need more powerful mechanisms supporting this new form of distributed computing? @izuzak

Comments