top | item 6360474

REST Hooks - Stop the polling madness

201 points| qwertzlcoatl | 12 years ago |resthooks.org | reply

112 comments

order
[+] annnnd|12 years ago|reply
Am I the only one who has trouble sifting through all this marketing? Even "read more" link leads to an article where half of the text promises what will be covered and another half talks about some subscriptions and how they are managed (they might have been mentioned in the first half, I agree - and I also read text only twice).

If someone has a better understanding of how this works, I would appreciate a TL;DR version for programmers (which are probably the target audience?). Thanks!

[+] bryanh|12 years ago|reply
We are pushing some copy updates to the homepage, most of the technical details were in docs. TL;DR coming to the homepage soon, but the gist is:

    REST Hooks itself is not a specification, it is a collection of patterns that
    treat webhooks like subscriptions. These subscriptions are manipulated via a
    REST API just like any other resource. That's it. Really.
Thanks for the feedback!
[+] crystaln|12 years ago|reply
And here I thought I was being dense.

Seems like it adds data that would normally be included in a poll response to requests that were going to be made anyway, and possibly adds requests that were normally going to be made independently to long polls, though I'm not sure about any of this.

If I am right, would be nice to explain this and how it compensates if there is not a "regular" request within a certain period of time. And well, to basically explain WTH REST hooks are....

Is the most important piece of information really: "REST Hooks itself is not a specification, it is a collection of patterns that treat webhooks like subscriptions"? That seems of tertiary importance at best. How about telling us what REST Hooks are and how they are that - not what they aren't from a marketing perspective.

This is targeting a technical audience. It shouldn't so drastically underestimate its readers.

[+] joeblau|12 years ago|reply
You are not the only one. I read though it, clicked around, looked at the demos, thought about bookmarking it, got to the documentation page and saw this `/api/v1/subscription/`, thought That breaks HATEOAS then left.

I'm sure if I put more time into trying to understand what is going on I could figure it out, but to me, the message isn't that clear.

[+] mcantrell|12 years ago|reply
Here's my basic understanding of it: This sounds like webhooks, but they have a set payload, so that the server that is sent the webhook just calls the rest endpoint it is given in the payload.
[+] pbreit|12 years ago|reply
Yeah, it's pretty bad. I think it comes down to "assuming WebHooks, a suggested 'pattern' to set up the subscription programmatically".
[+] mmgutz|12 years ago|reply
Agree with you. If the audience is developers, give it to us in black in white instead of some new jargon and marketing blah.

I looked through several pages (waste of time) and I still can't figure out how this is real time without websockets, polling, server side events. No way I would use this service if they don't know who their audience is.

[+] afandian|12 years ago|reply
Agreed. I came here to ask if anyone actually understood anything about the product from the front page.
[+] steveklabnik|12 years ago|reply
> REST Hooks itself is not a specification, it is a collection of patterns that treat webhooks like subscriptions.

Bummer. No matter if you like it or not, a collection of patterns with a name _is_ a specification, just possibly a poorly defined one. See the confusion in this thread. If it were a link to a spec, nobody would be confused.

> Skip the pedantic arguments about standards and implementation details

This reads to me as "everyone is going to have a slightly incompatible implementation. One library wont be good enough, I'll need to write a new one per site that uses this."

Furthermore, what about PubSubHubbub?

Finally, polling is great: Ive found few situations where it doesn't work well, people just tend to only do the most basic of implementations and blame polling. See http://roy.gbiv.com/untangled/2008/paper-tigers-and-hidden-d... for a really interesting example.

EDIT: numbers were real, added a supporting link

[+] technoweenie|12 years ago|reply
GitHub implements the PSHB subscription API. It's fine, but doesn't really fit in well with the rest of our JSON API. For instance, parameters are sent over as form encoded values like "hub.mode".

http://developer.github.com/v3/repos/hooks/#pubsubhubbub

We also have a regular JSON endpoint for our hooks resource (which is essentially a subscription API).

[+] bryanh|12 years ago|reply
Hey Steve!

> See the confusion in this thread. If it were a link to a spec, nobody would be confused.

We definitely had a little confusion in the thread, I think that was mostly because we put too much marketing on the homepage for this audience, but the absence of a proper spec could have definitely contributed. We're correcting some of this.

This is all about adoption of some sort of subscription-based HTTP callback: at this point in time any flavor will do. We have no doubt that a formal spec will pop up someday (and that would make us very happy!).

> Furthermore, what about PubSubHubbub?

PubSubHubbub always seemed a little heavy and was a departure from APIs that weren't XML/ATOM based (meaning most JSON API providers wouldn't touch it). Not saying I agree with it, but that is the feedback we got.

> Finally, polling is great for 99% of use cases (I can make up statistics, too)

We posted the numbers driving our stats on the homepage. They are not made up and come straight for our Elasticsearch cluster. I'm happy to elaborate on them!

[+] tkellogg|12 years ago|reply
This isn't impressive. [MQTT][1] is a much better protocol, and an open standard. It's a pub/sub that runs over TCP with variants that run over UDP & TLS. There's only a 2+ byte overhead, so it's very light. It's ideal for mobile devices because it has a QoS system built-in, so the developer doesn't have to think through the problem of missing messages where cell service is sub-optimal. MQTT is even resource-based, so it fits well with REST. I wish more people would start using MQTT instead of these crazy hacks like REST Hooks.

[1]: http://mqtt.org/

[+] bialecki|12 years ago|reply
If for some reason you can't or don't want to implement webhooks, at least make sure you the GET endpoint for any object has a query param that supports fetching the most recently updated or created objects and supports pagination.

It sounds trivial, but you'd be surprised how many APIs don't support one or both of those features. When you're writing an API it might seem unnecessary to start (after all, who could ever have 1000s of <object>?), but if someone ends up polling your API frequently, having those two features can reduce a lot of unnecessary load for both you and the poller. And, of course, make sure you have an index on the created and/or updated dimensions.

That said, webhooks are terrific. Few things to consider when implementing them:

- Think carefully about the payload you send to the webhook. It's usually a good idea to send some related objects/data because many times when someone gets a webhook payload, that'll trigger calls to your API to get related information you could've reasonably sent them in the initial payload.

- You'll likely want to some way to keep track of errors so if an endpoint starts returning 404s or 500s you have a way to programmatically discard it after X failed attempts.

- In your docs, give sample, "real world" payloads developers can test against. It saves times over creating a RequestBin, pushing there, copying, cURLing, etc. (Remember, you can't set up a webhook to localhost.)

- A nice to have is some sort of retry capability with an exponential back-off. Servers go offline and if they get pushed webhook data then, those messages are lost. You could say, "tough, it's the consumer's responsibility," but if having all the data is important, most people will resort to polling. (Somewhat related, you'd be surprised how often the APIs of some larger SaaS companies are "offline" -- e.g. returning 503 --, so these things do happen.)

[+] aeon10|12 years ago|reply
It is awfully difficult to get a technical TL;DR version from a website, whose target audience is programmers. I think explaining what this does concisely and technically, preferably on the front page with code examples etc would be really useful.
[+] bryanh|12 years ago|reply
We also wrote a quick drop-in Django application [0] (that plugs right into TastyPie if you have an API built on it) . We also have a sample CRM with a live API running [1]. Our friends at NetEngine wrote a sample Rails app [2], and our very own James wrote an awesome Node.js Sails app [3].

[0] https://github.com/zapier/django-rest-hooks

[1] http://demo.resthooks.org/

[2] https://github.com/net-engine/resthooks

[3] https://github.com/zapier/node-resthooksdemo

[+] aantix|12 years ago|reply
Still scratching my head on the Ruby "reference" implementation. When I subscribe resource_subscriptions, who is receiving the update notification? Is the client side js code getting a push? Is the resource_subscriptions a long-polling endpoint? Not very clear at all.
[+] mikeknoop|12 years ago|reply
Oh neat! We're launching resthooks.org today with several partners to promote an existing but underused real-time API pattern.

Here are more resources:

Intro: https://zapier.com/engineering/introducing-resthooksorg/

Why: http://resthooks.org/#why

Demo: http://demo.resthooks.org/

[+] _lex|12 years ago|reply
I'm looking for a quick response to "How", w/o going through code. Care to enlighten me?
[+] jalfresi|12 years ago|reply
I hate to be THAT guy but someone was going to say it so it may as well be me :)

This is a collection of patterns, right? Well why not make this really REST and rather than list a bunch of URL templates provide REL types for each of these like so:

REL subscriptions-list -> GET subscription-create -> POST subscription-read -> GET subscription-update -> PUT subscription-destroy -> DELETE

Now it doesn't matter what the URL structure is, I can pull the <link> elements from the page and be TOLD the URL, rather than follow a URL template. That way, this doesnt rely on out of band knowledge i.e this web page and its (poor) description.

[+] rcsorensen|12 years ago|reply
Has anyone use PubSubHubbub ( http://en.wikipedia.org/wiki/PubSubHubbub ) for this pattern in the past? For straight subscriptions, it's always been compelling to think of the use cases being covered by PSHB and RSS.
[+] julien|12 years ago|reply
So, PubSubHubbub is not linked with RSS anymore and works with any type of data format, including JSON obviously :)

The quick win of PubSubHubbub compared to Resthook is that it has a 'security' mechanism in it, which means that the subscriber can be 100% sure that the service sending the notification is not someone impersonating it.

Aside from this, I feel like PubSubHubbub and Reshook solve the same problem: programatically setting up webhooks.

[+] mcantrell|12 years ago|reply
This sounds like webhooks, but they have a set payload, so that the server that is sent the webhook just calls the rest endpoint it is given in the payload. Interesting idea, but it does sound like a regular webhook to me. That flow is the same way I use the Stripe webhooks for instance. I use the event id and make a call to Stripe directly, so that I can make sure the event is real.
[+] zrail|12 years ago|reply
I don't really understand what this is. Is this an attempt at a formalization of webhooks + JSON?
[+] mikeknoop|12 years ago|reply
Its not. Its promoting two things specifically:

1. Webhooks

2. A subscription layer via REST

Several major players already are doing this but it doesnt have a name.

REST Hooks are a way to consolidate that momentum and push it to a broarder audience.

[+] atonse|12 years ago|reply
Same here - I keep coming back to "are they just talking about webhooks? or are they also talking about some kind of persistent http streaming api?"

I'm thinking these are just Webhooks, but the REST part is throwing me off because I think of it more as a consumption concept (consuming resources, etc).

Update: Found this, which I think explained it best: "REST Hooks itself is not a specification, it is a collection of patterns that treat webhooks like subscriptions. These subscriptions are manipulated via a REST API just like any other resource. That's it. Really." - but it was in /docs, not the main page.

[+] bryanh|12 years ago|reply
It is just a pattern around making traditional webhooks another resource on your existing REST API so you can programmatically control subscriptions/callbacks/webhooks/etc. instead of asking users to copy-paste URLs.
[+] donpdonp|12 years ago|reply
Thats what it appears to be, but on closer inspection it doesn't go that far. They give an example rest path of /subscriptions but i cant tell if its an example or meant to be used as a standard endpoint name.
[+] bdcravens|12 years ago|reply
The way the info is presented bugs me. The first "case study" is Zapier. Later, in the "Learn how..." set of links, Zapier is listed as one of many. Look to the footer, and the Github link, and it's of course evident it's a Zapier project.

This project (er hmm, "initiative") is core to Zapier's business. If every service out there had a hook atop their service, it'd make things a lot easier for Zapier. That's cool. What bugs me is the feeling that Zapier's branding in the whole thing is less than transparent.

It seems that open source projects are getting more and more marketing driven, and the way this "initiative" is packaged is a sign of things to come.

[+] elliotanderson|12 years ago|reply
I don't see how a company being involved with this "initiative" affects the core concept - it's a net benefit for both API consumer and producer in moving to a subscribe/push model. Less requests, less load, closer to real time updates. Swap Zapier with any other consumer and the result is the same
[+] nkoren|12 years ago|reply
Curious if anybody can answer a question for me. I've recently started building some apps with MeteorJS, and am having loads of fun with it. At first I'd been a bit suspicious of its magic pixie dust -- seemed too good to be true -- but so far it's done everything it promised, and has allowed me to focus on developing the parts of the app I care about. After checking briefly to see that my data syncing was genuinely low-latency and low-bandwith (which under every test I've subjected it to, it appears to be), I've pretty much forgotten about this level of engineering and have focused on bigger-picture stuff.

Which brings me to the question: should I care about something like REST Hooks? Is it a mistake to become complacent and assume that the lower-level infrastructure will Just Work? Or can MeteorJS (and presumably other high-level frameworks) be trusted to handle this kind of stuff in a way that makes it safe for me to forget about it?

Just curious what the esteemed HN denizens think about this, as I'm sure there are some strong and reasonably well-informed opinions out there...

[+] 1qaz2wsx3edc|12 years ago|reply
I think, if anything, Meteor proves it's possible that it can be the responsibility of the framework, not the developer.
[+] prodigal_erik|12 years ago|reply
The docs talk about "event types" like contact.update which do not exist in HTTP and require knowing the semantics of a particular group of resources. To truly be RESTful, I would want "please notify me of any PUT, PATCH, or DELETE to this list of URLs, or POST creating a URL that matches this pattern." And if you're going to include a copy of the resource (I don't know what else a payload would be) you need the media type the subscriber expects as if you were doing content negotiation for a GET request from them. Or maybe the payload could just be the current result of a HEAD request so the subscriber can invalidate their caches, and they can send the appropriate GET request(s) when and if they care to.

There should be some standard link relation like <link rel="subscription"> so clients have some hint that this is available and where to request it.

I'd also want some way to manage the freshness/load tradeoff, like "please notify me within one hour but no more than every five minutes".

[+] hcho|12 years ago|reply
Doesn't this assume that the API will only be used from a module living under a URL?

Most REST apis these days seem to be consumed form client side.

[+] brian_cooksey|12 years ago|reply
The focus is on server-server communication. A client like a web browser would need to use a different approach to get notifications, but there are options in that space besides polling.
[+] donpdonp|12 years ago|reply
In addition to webhooks, using a websocket connection to listen for streaming updates is a great way to eliminate polling.
[+] dreen|12 years ago|reply
They are also easier to use and much more straightforward than hooks. And if you use something like Socket.IO the adoption of WS is irrelevant.
[+] retrogradeorbit|12 years ago|reply
Give a man a hammer, everything looks like a nail. REST has become a bit of a hammer. The reason a lot of RESTful designed software I've seen has such poor performance is because REST is used like a hammer... everywhere! And whole bunch of places it's not needed and it's not ideal.
[+] jalfresi|12 years ago|reply
I hate to be THAT guy but someone was going to say it so it may as well be me :)

This is a collection of patterns, right? Well why not make this really REST and rather than list a bunch of URL templates provide REL types for each of these like so:

REL subscriptions-list

[+] derek1800|12 years ago|reply
Are there patterns for client side JavaScript to be notified of changes to REST resources? I don't believe this particular pattern applies if I understand correctly as it needs a URL to notify.
[+] jordanthoms|12 years ago|reply
I think you'd have to have your server receive the hook, and then notify the client side using websockets/long polling etc. Pusher makes that really easy.
[+] bryanh|12 years ago|reply
Websockets are still your best bet here.
[+] lvh|12 years ago|reply
I don't agree with this part:

> In other words, if everyone implemented REST Hooks, server load for both the sender and receiver could be reduced by 66x.

No, the number of requests could be reduced by a factor of 66. I'm not saying that's not impressive, I'm saying that the polling requests that ended up resulting in no action are cheaper than actionable requests, so, server load will go down by much less than a factor of 66x. The amount of work is the same, just busywork is less.

[+] bryanh|12 years ago|reply
That is a good point. What kills us the most is the latency on network IO when polling.