This content originally appeared on DEV Community and was authored by Ashish Vaghela
Hey Folks!
Today we dive into the world of web hooks β the hidden heroes that make the web more interactive and exciting. Think of web hooks as a secret language that allows different systems to talk to each other in real time.
So what exactly is a web hook?
Imagine that you and your best friend have a special mailbox. If your friend has juicy gossip or a funny meme to share, they drop a letter into this mailbox.
You have the key, so you can receive and reply to the message immediately.
In the digital world, the web hook works the same way. Itβs like a unique URL (web address) that you give to another system. When the system has something important to tell you, it sends an HTTP request (like a digital letter) to your special URL. Your system receives the notification and can act based on the received information.
Why Web Hooks are the Coolest Kids on the Block
Web Hooks are like the superheroes of online communication. They swing by and save the day in a variety of situations!
1. Live Updates: You donβt have to wait for updates anymore! Web hooks allow you to get data into your system as soon as it happens. Itβs like a news feed that never sleeps.
2. Notes: Web hooks are the best messengers! They can send messages from one system to another faster than you can say βYou have email!β
3. Third Party Integrations: Web hooks are the glue that holds different services together. They allow you to connect and automate workflows across platforms like GitHub, Slack, and Stripe. Itβs like a team of digital assistants working seamlessly together.
Letβs see Web-hooks in action!
Enough talking, letβs code!
Here is a simple example of creating a webhook using Node.js and the Express framework.
const express = require('express');
const app = express();
// Parse JSON request texts
app.use(express.json());
// Define the webhook endpoint
app.post('/webhook', (req, res) => {
const message = req.body.message;
console.log('Message received: ', message);
res.status(200).send('Message received!');
});
app.listen(3000, () => console.log('Server running on port 3000'));
This webhook receives a POST request and logs it to the console.
In this example, if a POST request is sent to the /webhook endpoint with a JSON payload containing the βmessageβ field, the webhook will log the message to the console and send a response
And thatβs it.
Meanwhille we can connect over
https://www.linkedin.com/in/ashish-codejourney/
https://x.com/codejourney_
This content originally appeared on DEV Community and was authored by Ashish Vaghela