What are Webhooks?
A webhook is a lightweight API that powers one-way data sharing triggered by events. In other words, they are automated messages or triggers that an application sends whenever a desired event occurs.
For example, there’s a movie information application. We need to know whether any new movie has been released recently. In case of API implementation/polling, we need to keep making requests to the Movie application to know the details at any point of time. Even if there are no new movie releases, we need to be sending requests to the application again and again to keep checking for new releases which is redundant.
This is where the webhook comes into picture. With webhook implementation, the movie application will make a call to the Webhook in the application it is hooked to with required information whenever there’s an update. It means that the movie application will send a notification to the user whenever there’s a new movie release, making the process simple.
Technically, webhooks are “user-defined callbacks made with HTTP” according to Jeff Lindsay. The webhook url is generally a HTTP POST implementation. Whenever a desired event occurs at a hooked application, a HTTP POST request will be made to the webhook url and the application that has hosted the webhook will take necessary actions. The application hosting the webhook may have facilities to subscribe to and unsubscribe from the webhook to accommodate multiple applications as well.
Webhooks in Mule
While dealing with Webhooks in Mule, there are two aspects to it. One is to make a webhook callback and the other is to implement a Webhook using Mule.
1. Webhook callback from Mule application
As discussed earlier, webhooks are generally HTTP implementations. It is usually POST but in some cases, it is a GET implementation. Calling a webhook from a mule app is similar to sending a notification or an email. If this callback should not have an impact on the actual implementation of your application, it has to be carried out asynchronously. To achieve that, you can carry out this operation in an Async block. A simple HTTP Request component can be used to make the HTTP POST/GET call to the webhook url with the required headers and payload.
Here, we’ll be seeing how to call a Slack webhook from a mule application. Kindly refer to this link to create an incoming webhook in Slack. Now, we a webhook url similar to “https://hooks.slack.com/services/******/*******/*******".
In the below application, the slack webhook is hit whenever the order quantity is greater than 1000.
The result would be the order numbers being posted in the slack channel whenever order quantity is greater than 1000.
Here a basic example has been explained but Anypoint platform comes with 250+ of out-of-the-box connectors to integrate with various systems. Leverage the connectors, and create Mule Flows to consume the events from the source system and push the messages to registered callback URLs. Here we’ve made only one webhook callback. In case we need to make multiple callbacks to different webhooks in case of an event, we can have a subscription model implemented with facilities to subscribe to and unsubscribe from the event. Implement this as a REST API with endpoints to subscribe and unsubscribe. Once we have that, we can make a callback to all the subscribers whenever a desired event occurs.
2. Webhook implementation
Develop a webhook in mule and provide the url for apps/users to make a callback to whenever a desirable event occurs. Developing a webhook is similar to that of a REST API. Firstly, prepare an API Specification for the webhook with the endpoint and acceptable input structure. You can have a POST/GET endpoint based on the requirement. Then, implement the same using Anypoint studio with the operations to be performed whenever there’s a webhook callback.The webhook implementation can be a part of a certain application or you can have a standalone implementation for webhook as well.
In the below example, we have a simple webhook implementation where whenever a POST request is made to the HTTP listener at “/webhook” with acceptable input, it will perform a certain set of operations.
Instead, to follow api-led connectivity, you can first have the API for Webhook designed, implemented, registered and then made discoverable. Once the webhook is ready, the webhook url can be shared with users along with the acceptable input payload format for the request.