Wednesday, 25 November 2009

A simple PuSH client with Scala and Restlet

PubSubHubBub (PuSH) is an open protocol that enables a close to real time delivery of content in Atom and RSS feed. In PuSH terminology, content initiates from Publishers and is distributed to Subscribers via Hubs using a RESTful web service approach (everythings happens over HTTP). You'll find extensive documentation about the PuSH protocol and architecture on the PubSubHubBub site.

I am currently working on a simple PuSH subscriber written in Scala using the Restlet framework. The code is available at: It is a port of a Groovy version that I wrote a while ago. As I mentioned above, PuSH uses HTTP REST invocations, and to that effect the code leverages Restlet, a very good lightweight framework for writing RESTful web services in Java. I am planning on using RESTlet in 2 places. First is to implement the PuSH protocol itself as I need to provide GET endpoints used by the HUB to send subscribe/unsubscribe request and a POST endpoint used by the Hub to publish content. Second is to implement a simple admin REST API that I can use to query the state of subscriptions and add or remove subscriptions. Having the admin API makes it easy to implement front end UIs either in a web browser or as a command line using a tool such as Curl.

The port from Groovy to Scala was fairly effortless and both versions look actually very similar in terms of length and conciseness.

If you are new to Scala, there are few things to note if you look at the code.
  • You don't have to put all classes in separate files.
  • As Scala is based on the JVM, you can reuse existing Java libraries.
  • Scala supports traits which can be thought of Interfaces with associated code. A class can mix-it multiple traits thus providing the benefit of multiple inheritance without the complications.
  • Scala type inference is very powerful so in most cases no need to type variables. Parentheses are also optional in method calls.
Processing XML in Scala is quite a lot easier than in Java thanks to built-in support for xpath exposed with the \ method (which looks like an operator). Combined that with optional parentheses and you end up with very concise code. The code snippet below shows you how to extract the URL of a Hub from an PuSH enabled Atom feed.

In plain English, we find which of the link node within the XML document has a rel attribute set to hub. The result (hubXML) is an optional value; we use Scala's pattern matching to extract its actual value or react accordingly if the value does not exist. This may look a bit verbose but it is actually designed to reduce the amount of null pointer exceptions at run-time.

No comments:

Post a Comment