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: