Trigger Serverless DigitalOcean Functions with Hasura Events
DigitalOcean recently released DigitalOcean Functions, which is their serverless solution. It enables you to invoke and run functions when a web event happens.
In this post, you will see how to integrate DigitalOcean Functions with Hasura.
What Can You Use Digitalocean Functions For?
In the context of Hasura, you can use serverless functions for the following use cases:
Custom business logic via Hasura Actions
Event logic via Hasura Event Triggers
Scheduled / Cron triggers via Hasura Scheduled Triggers
We are going to look at the example of triggering DigitalOcean Functions with Hasura Events.
Create New DigitalOcean Function
Before creating a new function, you need to create a namespace. A namespace represents a collection of one or more functions.
To create a namespace, go to the "Functions" page and click on the "Launch" button.
Once the creation process is done, it redirects you to the newly created namespace, where you can add new functions.
Click on the "Actions" button and choose the first option - "Create Function".
To create a new function, you need to:
select a runtime
choose a function name
Setting a package name is optional, so you can leave it empty.
After configuring the new function, click on "Create". DigitalOcean creates a greeting function by default.
When you invoke the function, it returns a greeting:
"Hello <name> from Hasura!" where <name> is replaced with the name from the ?name= query parameter
"Hello stranger from Hasura!" when the ?name= query parameter is not present
If you invoke the function, you should see something similar:
For this article, the function is a good entrypoint to showcase the integration between DigitalOcean Functions and Hasura.
The function is triggered each time a new user is added to the database connected to Hasura. In other words, it greets each new user.
Configure Hasura Event
Hasura allows you to create event triggers on database tables. The event triggers capture events (insert, update, delete) on specified tables and then invoke an HTTP webhook to perform custom business logic.
In this case, the event trigger captures an insert event on the users table and then invokes the serverless DigitalOcean function.
The image below shows the Hasura application used for this article.
Create a new event trigger with the following details:
Trigger Name => greet_users
Database => default
Schema/Table => public/users
Trigger Operations => insert
In the "Webhook Handler" field, add the URL of your DigitalOcean function.
Then scroll down to modify the request options. It allows you to change the request method and the URL template. You need to change the URL template to pass the name of the newly created user.
For the "Query Params" section, choose name as the key and {{$body.event.data.new.name}}as the value.
After that, scroll until you see the "Create Event Trigger" button and click it.
Test DigitalOcean + Hasura Integration
It's time to test the event trigger. Add a new user to your Hasura application, as shown in the image below.
After saving the user, the event trigger should invoke the serverless DigitalOcean function. If you check the invocation logs of the event trigger, you should see the response from the DigitalOcean function.
You successfully invoked a serverless DigitalOcean function with the Hasura Event Triggers!
Conclusion
The example from the article is rather basic because the focus is on illustrating how to integrate the new DigitalOcean functionality with Hasura.
More examples of how you could use the serverless functions from DigitalOcean with Hasura Event Triggers are as follows:
sending push notifications and emails based on database events
transforming and loading data into external data-stores
deploying a static site with content backed by Postgres - whenever the Postgres data is updated, the site is rebuilt and redeployed automatically
However, the possibilities are endless. If you already use the two or you have some exciting ideas, you can reach out to us on our Discord server.
Check out the documentation to read more about Hasura Event Triggers. Alternatively, you can read more about DigitalOcean functions here.