Pages

Thursday, February 25, 2010

PubSubHubhub for NodeJS: Callbacks All the Way Down

NodeJS is a callback-based Javascript server API.

PubSubHubbub is a callback-based web protocol.

I put them together and the result is a PubSubHubbub client for NodeJS:

node-pshb on github


This project only includes a PubSubHubbub client interface, but to me that's the interesting part. You can specify an atom feed url, and functions to call when events happen on that feed due to PubSubHubbub.

The client library takes care of identifying the hub for that feed, requesting a subscription, and listening for subscription confirmation requests and feed updates from the hub.

It provides callback hooks for "subscribed", "update" and "error" events.

A simple client app looks like this:
var callbackPort = 4443;
var subscriber = new pshb.Subscriber(callbackPort);

// Start listening for subscription confirmation callbacks.
subscriber.startCallbackServer(); 

var topicUri = url.parse("http://localhost/foo"); // Dummy feed, always has updates

var feedEvents = subscriber.listen(topicUri);

feedEvents.addListener('subscribed', 
  function(atomFeed) {
    sys.puts('subscribed: ' + atomFeed.id);
  });

feedEvents.addListener('error', 
  function(error) {
    sys.puts('ERROR: ' + error);
  });

feedEvents.addListener('update',
  function(atomFeed) {
    sys.puts('got a PubSubHubub update: ' + atomFeed.id);
  });

I tested this out with the Demo Hub running on a local AppEngine launcher.

The demo app creates a a second server to host a dummy feed on port 80, so http://localhost/foo always returns a feed with the current time as it's "last updated." This is so the test hub always thinks there's an update ready for you.

So start the appengine with the hub running locally (in this demo it's assumed to be on port 8086), then run the test.js app, then go to your hub with your browser and manually update http://localhost/foo.

I noticed that I had to manually run some tasks in the hub's work queue so if you don't see any updates try checking the Task Queues in the app console for the hub. Run any pending "feed-pulls" and "event-delivery" tasks. I imagine there's a way to make them do that automatically but I haven't dug around enough in there to find it.

So there you go, NodeJS and PubSubHubbub: it's callbacks all the way down.

5 comments:

  1. I know about NodeJS and it's amazing evented IO, but I know nothing about PSHB. It might be nice to be explicit about the benefits of what you've done here. I do the same thing with my code, feeling that the benefit is astoundingly obvious, but most people don't even know what I've shown them. This is especially the case when your project depends on two very niche technologies: the people that know one library is small; the people who know them both are vanishingly small.

    ReplyDelete
  2. Good point, Josh- I guess after I write a bunch of code to do something I think is neat, I'm often too exhausted to write very much about why I think it's neat :)

    I'll write a proper post on this later, but I see NodeJS and PSHB as sort of parallel developments. They're both built on asynchronous callback notifications as a way to use resources more efficiently.

    Node is fast because it's non-blocking and evented. Everything is a callback. You register interest in specific events from sockets or other IO objects, and Node calls your callback whenever there's new data.

    NodeJS uses threads more efficiently than the standard blocking model.

    PSHB is a way to do callbacks between web applications, so you can register interest in some URL with a hub, and a hub calls your web application whenever that URL has updates. So you don't have to poll over and over, checking to see if anything has changed.

    PSHB uses network resources more efficiently than the standard polling model.

    I have a larger application in mind to demonstrate how this stuff can be used together but it's not ready yet. I figure I'll post some notes here as I make progress, even if it isn't immediately useful to anyone.

    This site is called --verbose for a reason ;)

    ReplyDelete
  3. This is awesome! I'm going to add a link to this to the PubSubHubbub wiki if you don't mind.

    ReplyDelete
  4. @onebigfluke.com Not at all, please do! :)

    ReplyDelete
  5. Hey. Are you part of the blogger team. Can you explain how I can get someone from blogger to address a complaint. I killed this blog addy, http://www.emptywoundhopefulheart.blogspot.com about three yrs ago. Some spammer reclaimed it, but they are using an unathorized pic of my small children who are minors. I'm pretty pissed off, especially since blogger seems to be ignoring the issue. Thanks.

    ReplyDelete