Ivan Zuzak – blog

RPC for Web Workers and distributed computing within the browser

Posted in web development by Ivan Zuzak on December 21, 2009

This blog has moved to a new domain. You can view this post here.

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:

// [worker object A - testingWorker.js]

// load pmrpc library

// expose a procedure
pmrpc.register( {
publicProcedureName : "HelloPMRPC",
procedure : function(printParam) {
              alert(printParam); } } );

// [window object B]

// load pmrpc library

// create worker
var testingWorker =
  new Worker("testingWorker.js");

// calls the exposed procedure
pmrpc.call( {
destination : testingWorker,
publicProcedureName : "HelloPMRPC",
params : ["Hello World!"] } );

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

5 Responses

Subscribe to comments with RSS.

  1. Bodo said, on March 18, 2010 at 3:08 pm

    Dear Bodo,

    I’m working in the EU-project ROLE and at the moment we are working on inter widget-communication. Now I have the problem, that I will send an message from the iFrame to the parent window. The iFrame content is on a other server, so the SOP impeded this. Is this communication possible with your PMRPC-script? I can’t find a hint for this on the websites.


    • Ivan Žužak said, on March 18, 2010 at 5:37 pm

      Bodo, that’s exactly the point behind postMessage and pmrpc – it works cross-domain i.e. SOP is not an issue. Access control is set by other mechanisms specific to postMessage. This is what “cross-domain” means in the project description.

      Therefore, pmrpc should work for the use-case you described. Let me know if you need any further and specific help – my e-mail is izuzak@gmail.com

  2. vprajan said, on October 10, 2010 at 3:39 pm

    I am currently working on a research project about having distributed functionalities inbuilt inside browser. while searching got to know about pmrpc, really interesting. It gives many impossible functionalities in javascript like window-window, cross-domain calls.. Really cool software.

    Like how you integrated web workers, is there any plans for integrating web sockets for inter browser communication as well ?

    I mean calling a remote js API running in another PC kind of feature. My central idea is to run web workers background in multiple PC’s and make them talk using message passing with/without central server..

    Let me study more about pmrpc first. :)

    • Ivan Zuzak said, on October 10, 2010 at 4:25 pm

      Vprajan — thanks for commenting!

      I’m happy to say that the kind of stuff you’re thinking about with distributed functionalities within browsers is exactly the kind of stuff I’m thinking about. :) Pmrpc is only the first step, but an important one. Can you tell me more about the research project you work on? I myself am a researcher (at the University of Zagreb) and I’m working on the Geppeto project — http://www.geppeto.fer.hr.

      Communication between browsers (or between workers in browsers) is something I’d like to have in the future and something I’m absolutely sure will happen (I’ve started working on this, but nothing to show yet). It will almost certainly be based on WebSockets. It is true that current this is very hard to do without some kind of central server (because browsers are clients not servers, it’s not easy to get an URI for processes executing within browsers), but this will change. Check out this blog post and comments below for some of my ideas: http://annevankesteren.nl/2010/03/peer-to-peer The post outlines a browser API for peer-to-peer communication.

      Let me know what you think when you get the chance of studying pmrpc in more depth. You can also contact me at izuzak@gmail.com.

  3. vprajan said, on October 10, 2010 at 3:44 pm

    sorry.. its not possible without a central server btw.. :)

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

%d bloggers like this: