Monday, December 30, 2024
Google search engine
HomeGuest BlogsWhat is a Webhook and How to Use it?

What is a Webhook and How to Use it?

Webhooks allow interaction between web-based applications through the use of custom callbacks. The use of webhooks allows web applications to automatically communicate with other web-apps. Unlike traditional systems where one system (subject) keeps polling another system (observer) for some data, Webhooks allow the observer to push data into the subject’s system automatically whenever some event occurs. 

This eliminates the need for constant checking to be done by the subject. Webhooks operate entirely over the internet and hence, all communication between systems must be in the form of HTTP messages.

Usage of Webhooks

Webhooks rely on the presence of static URLs which point to APIs in the subject’s system that must be notified when an event occurs on the observer system. An example of this would be a web-app designed to collect and manage all orders placed on a user’s Amazon account. In this scenario, Amazon acts as the observer and the Custom-Order-Managing Webapp acts as the subject. 

Instead of having the custom webapp periodically call Amazon’s API’s to check for a created order, a Webhook created in the custom webapp would allow Amazon to push a newly created order into the webapp automatically through a registered URL. Therefore, to enable the use of webhooks, the subject must have designated URLs that accept event notifications from the observer. This reduces a significant load on the subject as HTTP calls are made between the two parties only on the occurrence of an event.

Polling based systems vs Webhook based systems

Once the webhook of the subject is called by the observer, the subject may take appropriate action with this newly pushed data. Generally, the Webhooks are executed through POST requests on a specific URL. The POST requests allow additional information to be pushed to the subject. Additionally, it can also be utilized to identify among a set of various possible events instead of creating separate Webhooks URLs for each event.

Webhook Workflow

To implement incoming webhooks on your application, the following basic steps need to cover:

  1. Expose an API endpoint on your application server which accepts and processes HTTP POST calls
  2. Provide access to this endpoint for potential users of the webhook. The API endpoint will be called a data-source application whenever the relevant conditions are satisfied.
  3. Process the POST data and return a response to the webhook call initiator to indicate the status. This step may or may not be present.

Webhooks vs. APIs

Both Webhooks and APIs have the goal of establishing communication between applications. However, there are some distinct advantages & disadvantages to using Webhooks over APIs for achieving application integration. 

Webhooks tend to be better solutions if the following points are more relevant to the system being implemented:

  • If the data is updated on the server frequently, Webhooks tend to be better solutions as unnecessary API calls by the client to the server are eliminated. As per resthooks.com, 98.5% of API polls are wasted.
  • Webhooks allow better solutions for systems that require near-real-time data updates. API’s polls are usually run at pre-specified intervals which might prevent real-time data updates. With webhooks, updates are pushed by the server to the client as soon as the webhook is triggered.

API’s use should be preferred over Webhooks in certain other situations. The important aspects to be considered for the use of APIs over Webhooks are:

  • API’s use allows more customization of when to poll for data from a server and also how much data to poll from the server. The amount of data to be polled is adjusted through the APIs Polling Size. With webhooks, the server generally decides the data as well as the time at which it is pushed.
  • For systems with highly variable data (such as real-time systems, IoT systems, etc.), API-based polling might be a better option as for each API call, there is a high probability of actionable responses.
  • It is possible for data pushed by a server, over a Webhook, to be entirely ignored by the client in case the REST endpoints are offline. In the case that the server does not have a mechanism to retry such failed pushes, the data updates are entirely lost.

To address the possibility of losing data pushed by a server when the Webhook is offline, you can use an event messaging queue for storing such calls. Examples of platforms that provide such functionality include RabbitMQ, or Amazon’s Simple Queue Service (SQS). Both are designed to act as intermediate messaging storage facilities that avoid the possibility of losing a webhook call.

Webhooks in Slack 

Many popular platforms provide the functionality of creating Webhooks that might be used in custom applications. The messaging platform Slack also provides such a system to allow messages to be pushed into a user’s Slack account when called.

To create a Slack webhook URL, we need to set up an Incoming Webhook Integration through the Slack console as follows:

  1. Log in to slack.com from the account for which you need to create a Webhook
  2. Navigate to https://my.slack.com/services/new/incoming-webhook.
  3. Select a default channel on which the messages will be pushed.
  4. You can also specify details such as the account name and profile picture to identify where these pushed messages are coming from.
  5. Copy the generated Webhook URL

Adding an incoming Webhook in Slack

To push a message into the Slack channel, a POST request must be executed on the Webhook URL with the message to be sent specified in the request body. On successful execution of the request, the message should be pushed into the user’s Slack account. 

Pushing a message into Slack using a Webhook

Webhooks in Discord 

These days, many popular platforms provide the functionality of creating Webhooks using integrations that might be used in custom applications like Github, Circle CI. The community platform Discord also provides such a system to allow messages to be pushed into a user’s Discord server.

To create a Discord webhook URL, we need to set up an Incoming Webhook Integration through the Discohook as follows:

  1. Log in to https://discord.com/ from the account for which you need to create a Webhook.
  2. Create the server or use the existing one.
  3. Navigate to server settings and then select integrations -> create a webhook with the respective attributes.
  4. Select a default channel on which you want the message to be pushed.
  5. Copy the generated Webhook URL.

Adding the webhook 

We will use DiscoHook to push some notifications or urls released.

Discohook – Just give all the details of what we want our notification to be. 

What we do is create a custom message for our users to get all the required information they need to have. Like, let’s take the case: The students are preparing for interviews for their placements, we can help them with the following :

The discord bot will notify : 

Notification on Discord -> Click the link and progress.

Last Updated :
11 Aug, 2021
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

Share your thoughts in the comments

RELATED ARTICLES

Most Popular

Recent Comments