Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node and Express: How to implement basic webhook server

I'm having a surprisingly hard time finding tutorials. I'm new to webhooks and have not used or seen them, beyond some basic descriptions of how they're supposed to work.

Our use-case for this is updating users of our APIs when there are new records. Since we're using Kafka and have settled on "eventual consistency" , another use might be to notify them of errors when records could not be read/written properly from the Kafka stream.

So, the basic concept as far as I can see:

const express = require("express");
const router = express.Router();

const processSomething = callback => {
    setTimeout(callback, 20000);
}

router.post("/hook", (req, res, next) => {
    processSomething(() => {
        res.status(200).send({
            id: "ABC123",
            message: "New record added!"
        });
    });
});

module.exports = router;

Is this essentially what I'm shooting for? Would users post to this endpoint, await a response, then post again upon getting the response so as to re-subscribe? Is there a problem with this running for a long period or even indefinitely?

I could really use more sophisticated examples but I'm just not finding them. Most of what you find when Googling this involves integrating third-party webhooks, like Github, Slack, etc., not custom which is what I'm needing to build.

I'm not opposed to another approach entirely, either. Just searching for the best means of notifying API users of updates and other important info.

like image 667
Tsar Bomba Avatar asked May 08 '18 19:05

Tsar Bomba


People also ask

How do I setup a webhook server?

You can install webhooks on an organization or on a specific repository. To set up a webhook, go to the settings page of your repository or organization. From there, click Webhooks, then Add webhook. Alternatively, you can choose to build and manage a webhook through the Webhooks API.

How do I create a local webhook?

register the endpoint in the third-party application, then the third-application can send data to this endpoint continuously. I choose a request I want, and copy the data in its body, then send the data to the back-end using Postman. We can see the response in Postman, too. It helps us debug Webhook.


1 Answers

Webhooks try to notify users with delayed events but they do it in asynchronous way. Your code is synchronous that means users need to wait for reply. I think it should looks more like this

const express = require("express");
const router = express.Router();

const processSomething = callback => {
  setTimeout(callback, 20000);
}

router.post("/hook", (req, res, next) => {
  processSomething(() => {
    const webhookUrl = req.params.url;

    /**
     * Your Kafka action or something else. There
     * you should collect info about success or
     * fail of client's action.
     */

    /** 
     * Your API call to webhookUrl with 
     * your defined body about status of event
     */
  });

  res.status(200).send('OK')
});

module.exports = router;

where function processSomething do your action in asynchronous way and call client's API with response but your route still answer with response - 200 OK means you got the message from client.

Also you can delegate notyfing client about success/fail message to another place of your code but you need to save webhookUrl for that. In events system, you can create event (or simple message) where one of attributes is client's url.

like image 118
Grzegorz Gajda Avatar answered Sep 22 '22 20:09

Grzegorz Gajda