Webhooks in Mule
This article outlines the concept of webhooks, as well as how to make a webhook callback and implement webhooks in Mule.
Join the DZone community and get the full member experience.
Join For FreeWhat Are Webhooks?
A webhook is a lightweight API that powers one-way data sharing triggered by events. In other words, webhooks are automated messages or triggers that an application sends whenever a desired event occurs.
For example, say 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
When 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 have 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 the order quantity is greater than 1000.
Here, a basic example has been explained, but the Anypoint platform comes with more than 250 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.
We’ve made only one webhook callback. If 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
You can 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.
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.
Published at DZone with permission of Niladharshini Ayyasamy. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments