APIs That Make Things Happen

Embed Size (px)

DESCRIPTION

40 minute talk at GlueCon 2010 that tries to focus more on the Evented Web idea.

Text of APIs That Make Things Happen

  • 1. APIs That Make Things Happen WebHooks and the Evented Web came from my previous talk? expecting talk about npr api? how many are developers? there will be code...

2. @progrium#webhooks this is my twitter username and a handy hashtag if you want to reference this talk 3. quickly, about me 4. quickly, about me 5. What are webhooks?cant get very far without addressing this, since im sure most of you have no idea what webhooks are. theyre the basis of this talk. 6. When something happens, performHTTP POST with relevant data to a URLthat the user gives you. webhooks are event callbacks over http. the server/app calls your URL. thats it. its not a protocol, there is no standard. its more like ajax: just a useful design pattern. 7. command line: pipes. we used talk about the equivalent of pipes on the web and people say RSS! and I say nooo.... its something else. (webhooks) 8. Input Output Program pipes are amazing in their simplicity. its all from a bit of infrastructure involving input and output 9. STDIN STDOUTProgramSTDERRstdin, stdout were available to reroute wherever the user wanted most common use was chaining commands together: piping feedback loop, which is the key to emergent systems 10. xargswget echo mail grep wc catso you had all these simple little programs, that might not even be useful alone 11. cat xargswget echo mail grep wc string them together... 12. cat grepxargswget echo mailwc 13. cat grep mailxargswget echowc and you have something more useful than just the sum of the parts. remember this because well come back to it. 14. STDINProgram but it doesnt work without the output. it just breaks. 15. API Web App unfortunately thats how the web is today. we can talk to web apps, but they really cant talk to us. its as if you had a telephone system where you could only make calls, but never receive them. unacceptable. 16. API Events Web App its not that they cant, they just dont. we need to start placing event hooks in them. if this were as ubiquitous as apis are today, we would have something amazing: 17. Event-driven programming at the web ecosystem level event-driven programming... hence: 18. Evented Weba web where when something happens in one system, something can happen in another: trivially. youve all seen twitter to facebook (or vice versa). that would almost not be worth making a service for, it would just be a line of code. let me show you with another example: 19. function clickHandler() { alert("Click!"); }element.addEventListener('click', clickHandler); 20. element.addEventListener('click', function() { alert("Click!"); }); 21. element.addEventListener('click', function() { var name = $("input#name").val(); if (name != "") { alert("Hello, " + name); } }); 22. element.addEventListener('click', function(event) { var name = $("input#name").val(); if (name != "") { alert("Hello, " + name + ". " + "I'm " + event.target.id); } }); 23. twitter.addEventListener('newfollower', function(event) { var twitterUser = event.follower; var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser);} }) 24. twitter.addEventListener('newfollower', function(event) { var twitterUser = event.follower; var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser);} }) 25. twitter.addEventListener('newfollower', function(event) { var twitterUser = event.follower; var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser);} }) 26. twitter.addEventListener('newfollower', function(event) { var twitterUser = event.follower; var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser);} }) 27. twitter.addEventListener('newfollower', function(event) { var twitterUser = event.follower; var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser); } else if (twitterUser['following'] > 1000 &&twitterUser['followers'] < twitterUser['following'] / 2) { twitter.block(twitterUser);} }) 28. twitter.addEventListener('newfollower', v eventHandler); 29. twitter.addWebHook('newfollower', 'http://example.com/eventhandler'); 30. twitter.addWebHook('friendupdate','http://example.com/eventhandler'); some other events you could imagine writing handlers for 31. twitter.addWebHook('directmessage', 'http://example.com/eventhandler'); 32. twitter.addWebHook('myupdate','http://example.com/eventhandler'); makes twitter an even more powerful platform than it is 33. MAILHOOKSDEMO lets see this in action. mailhooks was one of the rst adapters i built for the evented web. in the evented web ecosystem, you can have very simple web services like this because integrating them together with webhooks is very easy... just like pipes led to simple programs. 34. facebook should be added. pop quiz! what do you get when you combine facebook and webhooks? 35. facehooks? 36. MORE DEMOS (and then code) create postbin, setup/show tender, pivotal tracker, twilio. demo clickhooks with postbin and and then show the code. http://2.latest.scriptletsapp.appspot.com/1w47Cs/run 37. webhooks are simple as you saw. their simplicity affords them to be used as a simple building block in slightly more complex systems like pubsubhubbub. 38. basically real-time feeds using webhooks as the core delivery mechanic. a specic use case of webhooks for new content updates. brad is here, he can tell you more... 39. all these sites publish content with pubsubhubbub, meaning they all effectively have webhooks for new content events... as a result, you can consume their content in realtime. 40. simple mechanics, if done right, yield rich, emergent dynamics. the emergent system with webhooks is the evented web. 41. The Evented WebProgrammable Web 2.0Web APIs Event Triggers Handler ScriptsWebHookshow i think of the evented web: at the top, you have key to pw 1.0. then webhooks, consist of two parts: triggers of events in apps... and handler scripts. the webhooks are usually the scripts, but i use it to talk about that hole side: trigger and handler. 42. In computer programming, hooking isa technique used to alter or augment thebehavior of [a program], often withouthaving access to its source code. theres a reason why its hooks, not callbacks or just events. i wanted to frame it with this idea. you can feed the result of a hook back into the system. this lets you build plugin systems etc that change behavior of web apps. 43. heres a video explaining hookpress, a plugin for wordpress that exposes their internal hooks as webhooks and what that can do. matt mullenweg realized this is how wordpress.com can have user plugins like the open source version... this is now deployed on wordpress.com 44. The Evented Web Programmable Web 2.0 Web APIs Event Triggers Handler ScriptsWebHooksi focus on the trigger side at talks since thats the hard part: getting people to put in event triggers. as an ecosystem you need infrastructure for this other side of handler scripts. 45. twitter.addWebHook('newfollower','http://example.com/eventhandler'); to the app that triggers it though, it doesnt matter. the idea is the handler is a URL... 46. twitter.addEventListener('newfollower', function(event) {var twitterUser = event.follower;var friends = facebook.getFriendsNames(); if (twitterUser['name'] in friends) {twitter.follow(twitterUser); } else if (twitterUser['following'] > 1000 &&twitterUser['followers'] < twitterUser['following'] / 2) { twitter.block(twitterUser);}}) but this code has to live somewhere. ill come back to this, but i wanted to touch on a point about this indirection via url 47. twitter.addWebHook('newfollower','http://example.com/eventhandler'); because its a URL you can just assume its a web app/script on the other end, which is *key* to why the evented web is about http webhooks instead of something like xmpp. it turns out... 48. HTTP is the easiestway to trigger codesince cgi in 1993, http is essentially rpc. in fact, its the most widely understood/used rpc in the world. now with everything in the cloud, web development being so popular, its the easiest way to get code to run. write a php script or ruby script. put it in the cloud for free, instantly. where? theres free php hosting, app engine, heroku ... but we can make it better. 49. if writing webhooks is to be ubiquitous, we need to make it EASY to write them. you dont need all of app engine to write a simple little hook script. so i imagined something like a pastebin site... only it runs code. 50. so i built scriptlets, which is basically that. use php, python, javascript to write simple little scripts hosted in the cloud. write it, save it, get a url to run it. perfect for webhook handler scripts. 51. heres a wrapper that makes postbin work for pubsubhubbub 52. heres a script used with hookpress to add comment notications via notify.io to wordpress 53. this is the code i used for the clickhooks demo. you can see how simple it is, notify.io does most of the work. 54. notify.io is a useful part of the ecosystem. it solves the notication part. how do you get events to the desktop? pubsubhubbub for example also a gateway drug for webhooks... 55. NOTIFY.IODEMO intro. twitter DM example. outlets. curl. NioCallback. DrEval... 56. What are webhooks?Event callbacks over HTTPenabling the Evented Web The Evented Web blends our existing ecosystem of web APIs with event-driven programming, creating a web that is both more programmable and real-time. 57. Infrastructure as Education im heavily interested in education -- hacker dojo, experiment in education. but also i think there are some huge lessons to be learned from hacker culture. one idea is thi