Slide 1

Slide 1 text

Working with Webhooks Lorna Mitchell, IBM May 2017

Slide 2

Slide 2 text

Webhooks in the Wild Webhooks power: • slack notifications • continuous integration services • other integrations @lornajane

Slide 3

Slide 3 text

How APIs Work @lornajane

Slide 4

Slide 4 text

How APIs Work @lornajane

Slide 5

Slide 5 text

How APIs Work @lornajane

Slide 6

Slide 6 text

How Webhooks Work @lornajane

Slide 7

Slide 7 text

How Webhooks Work @lornajane

Slide 8

Slide 8 text

How Webhooks Work @lornajane

Slide 9

Slide 9 text

What About Time? @lornajane

Slide 10

Slide 10 text

APIs Over Time @lornajane

Slide 11

Slide 11 text

Webhooks Over Time @lornajane

Slide 12

Slide 12 text

Webhooks Require PreArrangement @lornajane

Slide 13

Slide 13 text

Designing Webhooks @lornajane

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Receiving Webhooks @lornajane

Slide 17

Slide 17 text

Receiving Webhooks It's just a POST request! Advice: • DO: accept, store and acknowledge quickly • DON'T: validate or process before acknowledging @lornajane

Slide 18

Slide 18 text

Ngrok for Testing Webhooks https://ngrok.com/ - secure tunnel to your dev platform Use this tool to: • webhook into code running locally • inspect the request and response of the webhook • replay requests and see the responses @lornajane

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Storing Data @lornajane

Slide 21

Slide 21 text

Storing Data @lornajane

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Publishing Webhooks @lornajane

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Example App: Retro Guestbook @lornajane

Slide 27

Slide 27 text

Saving Data: Basic Process @lornajane

Slide 28

Slide 28 text

Saving Data: Handling Webhooks @lornajane

Slide 29

Slide 29 text

Saving Data: Handling Webhooks @lornajane

Slide 30

Slide 30 text

Data Hygiene Workers should be independent • pass in data up front if you can • ideally one component talks to each data store • keep metadata and data separate @lornajane

Slide 31

Slide 31 text

Saving Data: Handling Webhooks @lornajane

Slide 32

Slide 32 text

Saving Data: Handling Webhooks @lornajane

Slide 33

Slide 33 text

Saving Data: Handling Webhooks @lornajane

Slide 34

Slide 34 text

About Workers Workers are: • long running processes • may be able to process multiple message types • may have many of the same worker (horizontal scaling) • can be any tech (for bursty workloads, try serverless workers) Many microservices patterns also work well here @lornajane

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

Webhooks ... are awesome :) @lornajane

Slide 37

Slide 37 text

Thanks! • Slides: http://lornajane.net/resources • Example app: https://github.com/ibm-cds-labs/guestbook • RabbitMQ: https://www.rabbitmq.com/ • Bluemix: http://www.ibm.com/bluemix • Requestbin: http://requestb.in • Ngrok: https://ngrok.com/ • Website: http://lornajane.net @lornajane