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.

21 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
    Replies
    1. AngularJS Training in Chennai AngularJS Training in Chennai Node.js Training in CHennai Angular 2 Training in Chennai Angular 2 Training in Chennai Node.js Training in CHennai Node.js Training in chennai MEAN Developer Training in Chennai

      BackBoneJS Training in Chennai BackBoneJS Training in Chennai EmberJS Training in Chennai EmberJS Training in Chennai

      Delete
    2. A IEEE project is an interrelated arrangement of exercises, having a positive beginning and end point and bringing about an interesting result in Engineering Colleges for a particular asset assignment working under a triple limitation - time, cost and execution. Final Year Project Domains for CSE In Engineering Colleges, final year IEEE Project Management requires the utilization of abilities and information to arrange, plan, plan, direct, control, screen, and assess a final year project for cse. The utilization of Project Management to accomplish authoritative objectives has expanded quickly and many engineering colleges have reacted with final year IEEE projects Project Centers in Chennai for CSE to help students in learning these remarkable abilities.


      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      Delete
  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
  6. Hello There. I found your blog using msn. This is an extremely well written article. I will be sure to bookmark it and return to read more of your useful information. Thanks for the post. I’ll certainly comeback. TutuHelper

    ReplyDelete
  7. Most of the time I don’t make comments on websites. but I'd like to say that this article really forced me to do so. Really nice post! TutuAPp Andorid

    ReplyDelete
  8. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    hadoop training in chennai

    angularjs training in chennai

    ReplyDelete
  9. Kanhasoft is the top-notch Node.js development company in India. We have 45+ experienced programmers to delivered successful NodeJS Application development globally. Visit our site to know more about us.

    ReplyDelete
  10. If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state. organic ghee

    ReplyDelete
  11. I love your blog! You always have such interesting and thought-provoking content. google suite business

    ReplyDelete