Slide 1

Slide 1 text

WEBHOOKS PHPTour 2016

Slide 2

Slide 2 text

@guillaumepotier Cofounder & CTO Wisembly http://wisembly.com http://getsolid.io

Slide 3

Slide 3 text

Who never heard of webhooks? Should be a rhetorical question..

Slide 4

Slide 4 text

Who never used some?

Slide 5

Slide 5 text

Who already implemented webhooks in their application?

Slide 6

Slide 6 text

“A webhook in web development is a method of augmenting or altering the behavior of a web page, or web application, with custom callbacks. These callbacks may be maintained, modified, and managed by third-party users and developers who may not necessarily be affiliated with the originating website or application.” Wikipedia

Slide 7

Slide 7 text

“user defined callback made with HTTP POST” Jeff Lindsay

Slide 8

Slide 8 text

Push notifications / Reverse API

Slide 9

Slide 9 text

1. consumer sets up a server to listen/consume callbacks 2. consumer registers callback URL with provider 3. provider makes requests to URL when a registered event happens

Slide 10

Slide 10 text

That’s all! Easy right?

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

A few examples

Slide 14

Slide 14 text

Google calendar PUSH notifications

Slide 15

Slide 15 text

Google calendar PUSH notifications

Slide 16

Slide 16 text

Github webhooks

Slide 17

Slide 17 text

Github webhooks

Slide 18

Slide 18 text

Digging deeper

Slide 19

Slide 19 text

‣ a verb: POST ‣ an event: which could be subscribed by any user • for Github: pull_request, fork, commit, issues, etc.. ‣ a payload: containing the relevant data for the related event • often including: the resource itself, the sender (user who triggered the webhook) ‣ (optional) a security hash: to ensure webhook was delivered by the rightful authority • for Github: sharing a common secret used to generate a hash from the payload ‣ (optional) an ID webhook specs

Slide 20

Slide 20 text

‣ a CRUD API • a payload URL: the server endpoint that will receive the webhook payload • events list: which events would you like to subscribe to • (optional) the content type ‣ (optional) an history of recent deliveries ‣ (mandatory) a good documentation :) • try to mirror API resources • document possible events • document security mechanisms its registration

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Let’s implement it in your PHP application!* *We’ll see at least how we did it for Solid

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

https://getsolid.io

Slide 26

Slide 26 text

https://getsolid.io

Slide 27

Slide 27 text

https://getsolid.io

Slide 28

Slide 28 text

https://getsolid.io

Slide 29

Slide 29 text

1. when a meeting is ended 2. when a task is created 3. when a task is assigned Solid implements these webhooks events

Slide 30

Slide 30 text

‣ Various entities in your codebase could trigger webhooks ‣ Action that triggers webhooks could be in your services, your controllers, your commands. Pretty much everywhere ‣ You do not want to couple too much webhook logic from the rest of your application events to the rescue => use an event dispatcher symfony/event-dispatcher if you’re not using Symfony

Slide 31

Slide 31 text

events to the rescue ‣ We dispatch a single event listened by a WebhookSubscriber. In the DeliveryEvent we pass: • the event / action name • the related entity • the user that made the action triggering the webhook • (optional) changeset or other extra contextual params

Slide 32

Slide 32 text

‣ lookup for registered users for each webhook could be consuming ‣ sending many POST requests for potential registered users *is* slow and consuming ‣ the API call that creates, updates or delete a resources and triggers the webhook does not need at all to be aware of all that stuff and worse, waiting for hooked URLs answers! RabbitMQ for the scalability => no need to do that stuff synchronously, use AMQP btw swarrot/swarrot is a great lib for RabbitMQ

Slide 33

Slide 33 text

RabbitMQ for the scalability

Slide 34

Slide 34 text

Command Entity Exporter (Sender) (Extra) Registred hooks HTTPs POST … HTTPs POST

Slide 35

Slide 35 text

The command ‣ pros • well decoupled from the rest of the codebase ‣ cons • some extra queries (need to re-fetch entities we had in previous context) • works only for stored entities retrievable with unique ID

Slide 36

Slide 36 text

The command

Slide 37

Slide 37 text

The exporter

Slide 38

Slide 38 text

The exporter

Slide 39

Slide 39 text

Testing / Tools

Slide 40

Slide 40 text

ngrok

Slide 41

Slide 41 text

requestb.in

Slide 42

Slide 42 text

symfony2 profiler

Slide 43

Slide 43 text

Zapier

Slide 44

Slide 44 text

Connect with 400+ apps

Slide 45

Slide 45 text

Still some work to do… https://github.com/Wisembly/Zapier

Slide 46

Slide 46 text

Still some work to do… https://github.com/Wisembly/Zapier

Slide 47

Slide 47 text

Bonus

Slide 48

Slide 48 text

Leverage webhooks for your company

Slide 49

Slide 49 text

Thank you Questions ? https://joind.in/talk/3c147