Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
There are two methods that an application can use to receive real-time information about specific events happening in another application: polling and webhooks.
In this tutorial, we’ll learn about the two methods and then go into more detail about webhooks.
First, let’s see what they are:
In this method, the client API sends HTTP requests regularly to the server API and asks if a specific event has happened. Once the event occurs, the server API responds with information indicating the event has happened. This information is also called payload and is sent to the client API:
Webhooks send a message (payload) to the client API when a specific event happens on the server application:
Now let’s go into more detail about webhooks:
Now let’s compare two methods:
| Polling | Webhooks |
|---|---|
| Slower and use more resources | Faster and more efficient for both sides |
| Require more work on the client’s end | Require less work on the client’s end |
| Data transfer is not automatic | Since they’re event-driven, data transfer is done automatically |
In general, webhooks provide more advantages than polling.
Any application that needs real-time information about specific events happening on another application can use webhooks:
Webhooks need a webhook URL on the client side to be able to send HTTP requests to the client API. The HTTP requests are typically POST requests and need to be interpreted in the client’s backend:
Webhooks send data to the webhook URL in the client application. Further, the webhook URL is available to the public, which means that other than the webhook server, malicious users can also send fake data to the client application. To increase the security of the connection, we can:
In this article, we learned about different methods of sending information about real-time events between applications.