Photo by Efe Kurnaz on Unsplash
I’m the kind of person who practices a habit to explain technical terms with everyday examples so that I don’t need to memorize them just as a technical jargon. Maybe this process oversimplifies the term in some cases but who cares? As long as I understand how it works and doesn’t struggle to remember it, I think I’m fine, right? As a ‘Homo techians’ these days, we are bound to learn millions of things to keep ourselves up-to-date and it’s already hard enough to do that. So, some oversimplifications can’t hurt more.
So, as my attempt to explain ‘Webhook’ to you today, I’ll first tell you a ‘knock-knock’ story(not a joke — serious story) and then we will move on to the itsy-bitsy technical part. ready?
There was a guy, let’s call him Mr. Walter white. Yup, you got it — the nice guy from the TV series Breaking bad. By the way, our story isn’t from the TV series but will have the flavor of it.
Mr. Walter white
So, Mr. white wanted to start his own drug business and is in need of some illegal chemicals to do that. So, he contacted with some gangster guy, named Tuco Salamanca, who promised him to get the chemicals and they made a deal.
Tuco Salamanca (this guy is crazy but good at his job)
Some days over but Mr. white didn’t get the chemicals. So, he went to tucos house—
knock! knock!- who’s there?- It’s me, Walter white.- What do you want?- I’ve paid for some stuff and didn’t get them yet. - I know. You will get them soon. Now leave.
Walter white knew that this guy is crazy and he didn’t want to piss him off, so he left immediately. But after couple more days, he got impatient and came back again to tucos house-
knock! knock!- who’s there?- Me, Walter white. - what the @#$@ do you want?- where is my stuff?- I already told you, you will get them soon. - how soon? It’s more than a week. - Are you calling me a liar?- No, no. I just need an update. - I’ll let you know when the package arrives. Don’t ever come back again. Now get the hell out of here.(when Mr. white turned to go back, tuco called him)- Hey, just before you go, leave your $#@#ing phone number, so that i can call you.
So, Mr. white left his number. And the next day, he got a call from tuco that the package arrived.
Our little story ends here. Let’s now get back to the tech business and discuss webhook. Shall we?
We saw that in our story tuco taught Mr. white that he is comfortable with the “Don’t call me, I’ll call you” philosophy. Well, that’s the same philosophy lies behind the Webhook concept. You see, when we use some API (you know API, right?), we call it when we need it. The process works in a request-response manner. We will request for the data and the API will response. So, if there is some service API from which you need to get update regularly, you will call it frequently. The more frequent you call it, the more real-time update you will get. This is call ‘API polling’. But that’s not a good approach because most of the cases you will not get any update, as there aren’t any. Also, it’s a massive overhead for both sides in terms of resource usage. On the other hand, if these services have any webhook available then we just have to connect or ‘hooked’ our application URL with it and it will knock us every time there is our data available. Just like tuco did, when the package arrived, he called Mr. White. Simple as that.
Image Credit: thenewstack
And that’s all you need to understand how webhook works.
Neat, huh?
As most of the communications over the internet are done through the HTTP, both API and webhook use it extensively. But webhook doesn’t use all of the GET, POST, PUT, DELETE methods like API. It only makes an HTTP POST request (which also known as HTTP callback) to the listener or the service which wants to get the update. To the webhook, the listener will be just a simple URL which it will call with HTTP POST request for the specific event.
So, why would someone need a webhook or where do people use them?
Webhooks are mostly used for in cases where a real-time notification or action needs to be taken place for certain events, like slack notifications, continuous integration services, and other integrations. Webhooks can be used to exchange data between systems, components within a system, or microservices — anything attached via HTTP. Say, you built an application which will show you an alert if there is a possibility to rain today, so you can bring your umbrella. Or say, if somebody makes a commit in your project repository and you want to receive that as a notification in one of your slack channels. You see, in these cases, some actions are being taken place after some specific event occurs. So, for your weather application, you can connect it to some weather service webhook which will only send you a notification if there is a possibility of rain. If you want to get the real-time update in slack channel for commits, you need to connect your github account to Slack incoming webhooks so that it will receive messages on each commit.
Why are webhooks awesome? In the earlier era of the internet, all of the web applications work in a silo, and most them are still operating the same until these days. With the rise of the APIs, the communication and integration between these applications happened. But as we’ve seen in our example that a key part of this communication which is ‘event driven communication’ was missing. And Webhook comes to the rescue to save the day.
So, if you understand what I’m saying for the last of couple of minutes, then you’ll be fine with using the webhooks out in the wild provided by some fancy web services or applications. Or you can build your own webhook if need. They ain’t pretty hard. are they?