Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for server-side configurable Webhooks #6

Open
jkhoel opened this issue Sep 6, 2019 · 2 comments
Open

Add support for server-side configurable Webhooks #6

jkhoel opened this issue Sep 6, 2019 · 2 comments

Comments

@jkhoel
Copy link
Contributor

jkhoel commented Sep 6, 2019

Webhooks are user-defined HTTP POST callbacks. They provide a lightweight mechanism for letting remote applications receive push notifications from the backend, without requiring polling. For example, you may want any event updates to be pushed to a Discord channel, or some sort of messaging app.

@jkhoel
Copy link
Contributor Author

jkhoel commented Sep 6, 2019

There are apperantly a few ways to go about this:

I think the best way would be to have a global function available that could be inserted into any endpoint we would like to be exposed to webhooks.

@jkhoel
Copy link
Contributor Author

jkhoel commented Sep 6, 2019

Implementation like this:

config/webhooks.js:

module.exports = {
  POST_NEW_EVENT: (req) => {
    const { id, name, date } = req.body;
    const url = `www.someService.com/events/new?id=${id}&name=${name}&date=${date}`

    return new Promise((resolve) => {
      HTTPrequestToService(url) // Make the request to the url
      resolve(true)  // or some other result
    })
  },
  SOME_OTHER_HOOK: () => null,
}

route/events.js:

// Import the webhooks config
const webhooks = require('../../config/webhooks');

router.POST('/new', (req, res) => {
  // Trigger a webhook
  webhooks.POST_NEW_EVENT(req);

  // ... do further SQL queries and actual endpoint stuff
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant