My Rhinoceros likes WebSocket, not leafs

  • w3c • java • javascript • rhino • api • specs • websockets
  • 399 words

Last week I started implementing W3C Sockets API into a Rhino based JavaScript runtime.

It seems like plumbing API from one environment to another is one of the activity I do the most. And if you dare doing a joke about my Italian-ness and Super Mario… you will be pointed in the direction of the door.

Here I want to share a couple of findings.

Rhino is not bad at all

Yes, I said it. And who has worked in the past with me, knows how my opinion are hard to change. But they do sometimes, fortunately.

a big one...

I’m accustomed to QtScript and QtWebKit: those are the javascript runtime API/environment I used the most. But I have to give credit to this very good piece of software from the Mozilla Foundation.

In facts, Rhino is not bad at all. I was initially very dubious of his quality (Java, ehm…) but I have to slap my own hand: it not only provides good execution time, but it also has a very, VERY nice API. And Neustar Webmetrics runs it’s whole business on it!

What I liked the most? 2 things:

  • One, the fact that to create a JavaScript read-only property named, say, prop you just have to provide getProp method. And if you want the property to be writable, provide a setProp. The engine will take care of mapping L-VALUES or R-VALUES usages to the correct getter/setter.
  • Two, the NativeFunction Class is neat and easy to use: extremely simple to allow the scripter to provide an event handler. Almost brain-dead-simple.


Nathan Rajlich has made available on GitHub a good (not yet complete, though) implementation of WebSockets in Java. The API is simple, reflects more or less the W3C specs, and does the job.

the fun of WebSocket handshake

I used it with a couple of Node WebSocket implementations (miksago’s and einaros’), and it’s the only one that managed to negotiate a connection and transmit stuff without issues.

There are not many implementation of WebSockets out there yet (particularly for Java). I believe the main reason being the unstable and still-changing specs (take a look at this table).

So, KUDOS to Nathan!

P.S. A beer if you can write in the comment where the name Rhino originally comes from. A hint: you must have studied JavaScript BEFORE jQuery was cool and XHR was yet to become a superstar.