Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.


How to create a simple yet powerful webhooks mechanism in your PHP Application.

  • Login to see the comments


  1. 1. WEBHOOKS PHPTour 2016
  2. 2. @guillaumepotier Cofounder & CTO Wisembly
  3. 3. Who never heard of webhooks? Should be a rhetorical question..
  4. 4. Who never used some?
  5. 5. Who already implemented webhooks in their application?
  6. 6. “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
  7. 7. “user defined callback made with HTTP POST” Jeff Lindsay
  8. 8. Push notifications / Reverse API
  9. 9. 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
  10. 10. That’s all! Easy right?
  11. 11. A few examples
  12. 12. Google calendar PUSH notifications
  13. 13. Google calendar PUSH notifications
  14. 14. Github webhooks
  15. 15. Github webhooks
  16. 16. Digging deeper
  17. 17. ‣ 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
  18. 18. ‣ 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
  19. 19. Let’s implement it in your PHP application!* *We’ll see at least how we did it for Solid
  20. 20.
  21. 21.
  22. 22.
  23. 23.
  24. 24. 1. when a meeting is ended 2. when a task is created 3. when a task is assigned Solid implements these webhooks events
  25. 25. ‣ 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
  26. 26. 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
  27. 27. ‣ 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
  28. 28. RabbitMQ for the scalability
  29. 29. Command Entity Exporter (Sender) (Extra) Registred hooks HTTPs POST … HTTPs POST
  30. 30. 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
  31. 31. The command
  32. 32. The exporter
  33. 33. The exporter
  34. 34. Testing / Tools
  35. 35. ngrok
  36. 36.
  37. 37. symfony2 profiler
  38. 38. Zapier
  39. 39. Connect with 400+ apps
  40. 40. Still some work to do…
  41. 41. Still some work to do…
  42. 42. Bonus
  43. 43. Leverage webhooks for your company
  44. 44. Thank you Questions ?