Trigger Cloudflare Workers from database changes
How to use a webhook sink to trigger Cloudflare Workers from database changes.
Cloudflare Workers are serverless functions that run on Cloudflare’s global network.
Often, you want to trigger a Cloudflare Worker when a database row changes. For example, you may want to trigger a worker as a side-effect of a database change, or fan out work to multiple services.
In this guide, you will learn how to setup Sequin to trigger a Cloudflare Worker when a database row changes.
Prerequisites
You are about to create a simple Cloudflare Worker that logs a message to the console. You’ll trigger this worker by setting up a Webhook sink in Sequin that sends a HTTP POST request to the worker’s URL with the payload of the database row that changed.
You’ll need the following:
- A Cloudflare account
- Sequin installed locally or a Sequin Cloud account.
- A database connected to Sequin
Create a Cloudflare Worker
Start by creating a new Cloudflare Worker that takes in a Sequin change event as a payload and logs the payload to the console.
Create a new Cloudflare Worker
- Log in to your Cloudflare dashboard and navigate to the “Workers and Pages” section.
- Click on “Create Worker”.
- Give your service a name (e.g., “user-worker”) and click “Deploy”.
Cloudflare will create a new worker.
Add worker code
Click the Edit code button.
Replace the default code in the worker editor with the following:
This worker first checks the authorization header to make sure the request is coming from Sequin. Then it processes the payload and logs a message to the console. In this example code, the row that changed contains a name
field, so the worker logs the name to the console.
Click the Deploy button to deploy your worker.
Add the SEQUIN_WEBHOOK_SECRET environment variable
- In your worker’s settings, go to the “Settings” tab.
- Scroll down to the “Variables and Secrets” section.
- Click ”+ Add”.
- Set the variable name as
SEQUIN_WEBHOOK_SECRET
and the value to a secure secret of your choice. - Click “Deploy”.
Keep this secret handy. You will need to use this secret value in the Sequin dashboard when you create the push consumer.
Get the worker's URL
Under the “Settings” tab, find the “Domains & Routes” section.
Copy the worker’s URL. You will need this URL in the Sequin dashboard when you create the push consumer.
You’ve successfully created a Cloudflare Worker that logs a message to the console when Sequin sends a HTTP POST request to the worker’s URL.
Create a webhook sink
Create a webhook sink that captures changes to your database and sends a HTTP POST request to the Cloudflare Worker’s URL:
Create a new sink
Navigate to the “Sinks” tab, click the “Create Sink” button, and select “Webhook Sink”.
Select source table
Select the table you want to capture changes from (i.e public.users
).
Choose message type
Specify whether you want to receive changes or rows from the table.
If you’re not sure which to choose, leave the default setting of “Changes”. Changes are a better fit for event-driven use cases, like triggering workflows, which is usually what you want to do with webhooks.
Specify change types
If you selected changes, in “Records to process”, you can indicate whether you want to receive insert
, update
, and/or delete
changes. Select all three for now. No need to add a filter.
Configure backfill
You can optionally indicate if you want your webhook endpoint to receive a backfill of all or a portion of the table’s existing data. For now, leave “Backfill” toggled off.
Configure message grouping
Under “Message grouping”, leave the default option selected to ensure events for the same row are sent to your webhook endpoint in order.
Configure delivery settings
Under “Delivery configuration”, choose a conservative value for “Request timeout” and set “Batch size” to 1 since we’re just logging messages.
Configure HTTP endpoint
Under “HTTP Endpoint”, enter the Cloudflare Worker’s URL you copied earlier. Then click to “Add Encrypted Header” with the key Authorization
and the value Bearer SEQUIN_WEBHOOK_SECRET
, using the secret value you set in your worker’s environment variables.
Name and create sink
Give your sink a name (i.e. users_webhook_sink
) and click “Create Webhook Sink”.
Your webhook sink is now created and ready to send events to your Cloudflare Worker.
Test end-to-end
Create a row in your database
For example, insert a row into the users
table:
Trace the change in the Sequin dashboard
In the Sequin console, open the Messages tab on your webhook sink and confirm that a message was delivered.
Confirm the event was received by your Cloudflare Worker
- Go to your Cloudflare dashboard and navigate to your worker.
- Click on the “Logs” tab.
- You should see a log entry. For example:
Hello John Doe
:
You’ve successfully triggered a Cloudflare Worker from a database change!
Next steps
Modify this example to suit your needs:
- If you need to run long-running jobs, consider using Cloudflare Durable Objects in tandem with Workers.
- Tune your webhook sink configuration to suit your volume of work.
- Implement additional security measures, such as Cloudflare Access for enhanced authentication and authorization.
Was this page helpful?