Before we dive into it, let’s just imagine you just gave someone a cheque and you wanna be informed as soon as the money is deducted from your account. Now you can either
- Continuously call the bank’s teller, ask about your account balance or whether cheque came in or not.
- Wait for a simple message that would inform you about the transaction.
Keeping this in mind let’s discuss webhooks.
Webhooks are special HTTP Callbacks that inform us about any action that has taken place somewhere. These are incredibly useful when we want to get notified about an event, right when it occurs.
Webhooks are also known as Reverse APIs as rather than client applications sending some info to a server (like an API), the server notifies the client application about some action that took place. This greatly reduces the load on the server.
Without webhooks, we need to continuously ping a server to know the status of any transaction. This process is known as Polling.
Polling is a resource hoarding process and as the number of devices increase, the server could get extremely cluttered with such requests. This could eventually lead to an increased cost of infrastructure and delays in the request.
Webhooks operate on the concept of ‘Event Reaction’. This means that the client is only informed when an action takes place.
For a webhook to work we need to provide an endpoint URL for Client-Side application to the Server-side application. An endpoint is a URL through which we can get access to the web application. The server consumes this endpoint URL when it needs to notify the client about some action.
While we can send some additional information/payload (eg, JSON data or XML) through webhooks, but its primary goal is to act as a notifier and it is a good practice to limit it to that only. Also, we should have a separate mechanism of receiving a webhook and processing it. For more information on this please refer to this article.
So coming to the scenario discussed above. It is always better to get notified about an event that has happened. You might think what’s the harm in continuously checking the status, but you need to realize that you are not the only device/resource an application is going to serve.
Webhooks allow us to implement better, efficient applications. From getting your Bank transaction’s information to, updating the WhatsApp's delivery status in real-time, every application uses Webhooks in one way or another.
This was Part 1 of the webhook tutorial. In Part 2 we are going to implement Github Webhook with Python. You can find the article here.