Webhooks Example
This example shows how to handle external service webhooks in our application. In most of the cases external services expect your webhook handling code to be fast and failsafe. What you're expected to do is to enqueue handling of the webhook somewhere in your database/queueing system and handle asynchronously. With LambdaQueue you can just forward webhook payload to one of our queues and handle it later.
Architectural overview
We will forward each webhook event type to separate queue. The only reason to return error to webhook caller would be if LambdaQueue refused to queue item. All application level errors will be handled in LambdaQueue processing code.
Code example
import lq from '@dayone-labs/lambda-queue-express'
import express from 'express'
import './models'
const router = express.Router()
const userCreatedQueue = lq.queue('/userCreated', async (event: UserPayload) =>
userService.createUser(event)
)
const userUpdatedQueue = lq.queue('/userUpdated', async (event: UserPayload) =>
userService.updateUser(event)
)
//Just push different webhook events to separate queues, we'll handle them later
router.post('/webhook', async (req, res) => {
const event = req.body
try {
if (event.type === 'user.created') {
await userCreatedQueue.push(event)
res.send('OK')
} else if (event.type === 'user.updated') {
await userUpdatedQueue.push(event)
res.send('OK')
}
} catch (e) {
res.status(500).send(e.message)
}
})
//Queue is an Express router, just mount it with app.use(queue)
router.use(userCreatedQueue)
router.use(userUpdatedQueue)
export default {
route: router,
}
Remarks
INFO
👋 Webhooks can sometimes overwhelm applications or get lost when app has a downtime. We are working on creating an external webhook handling API. You then would only need to handle items from LambdaQueue in asynchronous way. Get in touch with us to let us know if you would like to test it out.