Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Wonderful World of Webhooks

The Wonderful World of Webhooks

Lorna Mitchell

August 24, 2016
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. Webhooks in the Wild Webhooks power: • slack notifications •

    continuous integration services • other integrations @lornajane
  2. Webhooks in the Wild: GitHub "ref": "refs/heads/master", "before": "1ae6a404351cead52df24893621d82ba6ec84a1c", "after":

    "e8474d83985330fa36f8862b37ca84ada4313392", "created": false, "deleted": false, "forced": false, "compare": "https://github.com/lornajane/demo/compare/1ae6a404351c...e847 "commits": [ ... ], "repository": { ... }, "pusher": { ... }, "sender": { ... } @lornajane
  3. Designing Webhooks Consider the use cases: • try to include

    all information for common outcomes • consider impact of payload size vs potentially many followup API calls @lornajane
  4. Receiving Webhooks It's just a POST request! Advice: • DO:

    accept, store and acknowledge quickly • DON'T: validate or process before acknowledging @lornajane
  5. Storing Data Simplest queue can go in your database! •

    an ID • the payload that came in (extract fields to search on) • a status field (new/processed/failed) A cron job to work through what needs doing and update the status field. @lornajane
  6. Storing Data If you outgrow the database method, use a

    queue. • e.g. RabbitMQ, Beanstalkd, Gearman • easy way to link many workers with work to do @lornajane
  7. Publishing Webhooks Offering webhook integrations is ideal if: • you

    have clients polling your API a lot • it's common for another system to react to changes in your system • you want to offer notifications for specific events • any of the above apply either internally or externally @lornajane
  8. Example App: Retro Guestbook In the olden days, we had

    guestbooks on our websites. • Allow user to leave their name and a comment • Show the comments left so far • Include webhook management for notifications of new comments @lornajane
  9. Demo Setup • An endpoint that we can send webhooks

    to for testing purposes: http://requestb.in/ • Queue is RabbitMQ (https://www.rabbitmq.com/) • RabbitMQ's management plugin has a web interface: http://192.168.121.8:15672 • (comments also go into the database that powers the site) • Worker scripts to send any/all webhooks @lornajane
  10. Example App: Use a Queue $connection = new \PhpAmqpLib\Connection\AMQPConnection( $host,

    $port, $user, $pass); $channel = $connection->channel(); $channel->queue_declare('comments', false, true, false, false); $msg = new \PhpAmqpLib\Message\AMQPMessage( json_encode($comment), ["delivery_mode" => 2] ); $channel->basic_publish($msg, '', 'comments'); @lornajane
  11. Example App: Workers Use The Queue amqp.connect('amqp://localhost', function(err, conn) {

    conn.createChannel(function(err, ch) { var q = 'comments'; ch.assertQueue(q, {durable: true}); ch.consume(q, function(msg) { // make the POST request for each webhook }); }); }); @lornajane
  12. Example App: Workers Send Webhooks // hooks populated from couchdb

    hooks.forEach(function (url) { request({ url: url, method: "post", json: msg.content.toString() }, function (error, response, body) { // log hook outcome }); }); ch.ack(msg); @lornajane
  13. Webhooks in Your Applications • Use them WHEN you want

    to notify other systems • Examples of HOW to use webhooks hopefully gave you some ideas • Webhooks are HTTP: we already understand this @lornajane