Webhook – An Interesting way to commune between applications

Reading Time: 5 minutes
Image for post

In this we are going to talk about a way or method to communicate between apps over the web which is gaining popularity nowadays. As we know, Polling is one of the ways by which we can communicate between apps to share information. There is also another way by which we can communicate i.e., WEBHOOK.

Webhooks are automated messages or information that an app sends to other apps. As in layman terms we can say that Polling is like asking for information repeatedly after a fixed period of time or when wanted whereas Webhooks are that whenever we have information to share then we go to others to give it to them. Likewise, Webhook is a way for an app to send real time information to other apps. In this way the webhooks are more efficient for the sender and the receiver as in Polling app constantly checks other app for real time information but in Webhooks other app sends the information to us whenever it is available.

Example of sending data over web

How does Webhook work?

Now that we know what webhooks are, let’s get to know how the webhooks work. As we get to know that webhooks send automated messages to tell about the information, so that other apps can use the data and complete the task which needs the data. This data is sent over the web by the app from where the information is generated, to the app which needs the data. Let’s see the visual representation of this:

Visual Representation of webhook working

This data is sent over the web through a “Webhook URL”. This URL is generated by the application which is on the receiving end. This acts as the address where the data needs to be delivered by the sender application.

Format

The format used to send data through the webhooks is either in JSON or XML. The request is done as an HTTP POST request. An example for webhook may look like this:

Format of Webhook

Why use Webhooks?

Now let’s discuss why should we use webhooks. Let’s assume that we need to store data on the user logged in the system on the database application through the login portal. If we can get the notification or data on the user whenever they login to the system through the portal then we can easily check the no of users logged in in the system. One way of doing this is webhooks. We can get the webhook URL from the database application and then send the data from the login portal to the database application, which can then update the database. Here an image that can help to visualize the above scenario.

Scenario on using webhook

Advantages

As we have read that Polling is like asking if the information is available or not and webhook is giving the information whenever available.

In real world example we can understand polling and webhook as when we go to a store to buy new watch of our favorite brand but get to know that the stock is cleared, so in that case we go to retailer to check if the stock is available or not; Whereas there may be a store or suppose an online store, they tell you to leave your phone number so they can contact you whenever the stock is available. Same thing happens in polling, we constantly ask the provider application for information. But in webhook, the provider gives us the info whenever available. This also is an advantage of webhook over polling, in polling we constantly have to use resources to ask the provider for data but in webhook provider tells whenever data is available which helps in saving the resources and time for both sides.

Advantage of webhook over polling

Steps to create a Webhook

With webhooks, it is generally three steps process:

  1. Getting the webhook URL from the receiver application.
  2. Use the URL in the webhook section of the provider application.
  3. Choose the type of event at which the provider needs to notify the receiver.

Despite that nowadays webhooks are getting popularity due to their feature some applications still do not support webhooks. In these cases, there are many middleware applications such as Zapier, Automate.io that have built-in integration that “poll” the application and send data via webhooks. This helps applications not having webhook integration to connect and share the data with other applications.

Some sites using Webhooks

Below are some examples of applications which use webhooks to connect and share data over the web.

  1. GitHub – GitHub uses it to tell applications about the changes or action taken in the repositories.
  2. Foursquare – Foursquare uses webhooks to tell applications whenever a user checks in.
  3. Stripe – Stripe uses a webhook to tell other applications whenever a user pays through stripe.

Adding a GitHub Webhook in Jenkins

To understand more about Webhook we can try set one up for GitHub. GitHub webhooks in Jenkins are to trigger a build if there is any change in the main branch of the repository.

Steps for adding Webhook in GitHub

  1. Go to your repository.
  2. Go to “settings” section.
  3. Click on “webhooks” on the left side panel.
  4. Click “Add webhooks” button.
  5. Fill the form shown in the image below 

In the “Payload URL” write https://{your jenkins url}/github-webhook/. You can disable SSL verifications if you don’t have a valid SSL cert for your Jenkins server.

Note: If you are running Jenkins on local system then add “https://localhost:8080/github-webhook/ “, it will not work because Webhooks works when exposed on the Internet.

  1. Then, select the event on which webhook should trigger.
  2. Finally, click “Add webhook” button.

Building pipeline in Jenkins to handle the webhook

  1. Go to “Manage Jenkins” then “Configure System“.
  2. Scroll down and you will find “GitHub Pull Requests” checkbox. In the “Published Jenkins URL“, add your repository link.
  3. Click “Save” button.
  4. Now go to Jenkins pipeline and select “GitHub hook trigger for GITScm polling” in “Build Triggers” section. 

Now, it will build the pipeline with every push to the configured branch.

Conclusion

Webhook is an important part of the communication between applications over web. It allow us to save resources, time and facilitates quick data transfer. Best way to understand about Webhook is to try it.

Thanks for keeping up

Written by 

Shivam Gupta is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a keen interest toward learning new technologies. He has a decent knowledge of Java Language. His practice area is DevOps. When he is not working, you will find him watching anime or with a book.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading