Sometimes, you may find the need to be notified about changes in your Supabase database as soon as possible. It can be for various reasons. For example, you may want to know if a new user registered, or if a critical business action occurs, such as a user adding an item to their shopping basket. This tutorial outlines a simple way to achieve this using Emitbase (an open-source alert platform for developers).
Prerequisites
- Node.js 18.12 or later.
- Docker on your machine.
- Installed Emitbase CLI.
Supabase Table and the Requirements for Alerts
For the sake of simplicity, I have a basic table named buyers
with the following columns:
-
id
(the table's identifier) -
created_at
(the registration time of the user) -
updated_at
(the time of the user's last update) -
name
(the buyer's name) -
approved
(a flag indicating if a user has been approved by an admin and can log into the application)
I want to receive a Slack message when the following events occur:
- A new buyer registers within the last five minutes.
- The 'buyers' table contains any rows where the 'approved' column has a 'FALSE' value.
Step 1: Create a New Emitbase Project
$ npx emitbase init <your-project-name>
Step 2: Set Up Your profiles.yml
First, create a credentials file:
$ touch profiles.yml
In the profiles.yml
file, please define your credentials:
emitbase:
databases:
dev:
host: <database-host>
database: <database-name>
port: <database-port>
user: <database-username>
password: <database-password>
notifications:
dev:
slack:
port: <slack-port>
channel: <slack-channel>
signingSecret: <slack-signing-secret>
token: <slack-token>
target: dev
Check the Integration with Slack documentation page for more information about Slack credentials.
Step 3: Create Necessary Thresholds for Alerts
The first threshold must track whether a new buyer has registered within the last five minutes:
new_users:
expression: SELECT * FROM buyers WHERE created_at >= NOW() - INTERVAL '5 minutes'
cron: '* * * * *'
The second threshold must track whether the 'buyer' table contains any rows where the approved
value is FALSE
.
unapproved_users:
expression: 'SELECT * FROM buyers WHERE approved = FALSE'
cron: '* * * * *'
Step 4: Create Alerts Messages
For new users:
new_users:
slack:
message: 'You have new users who have registered on the platform! 🚀'
For unapproved rows:
unapproved_users:
slack:
message: '📣 We have some unapproved users! Take a look!'
Step 5: Build and Run Emitbase
To run the project, it is necessary to build a Docker image:
$ docker build -t emitbase .
Then, you can just run it and wait for notification on your slack! 🎉
$ docker run -it emitbase
Showcase
The following images show messages in Slack. It's really simple; you can set up your alerts in minutes!
Conclusion
For more information, please refer to the entire Emitbase documentation. If you encounter any issues, please feel free to create an issue. Lastly, if you appreciate Emitbase and would like to show your support, please give it a star on GitHub. Thank you!
Top comments (0)