DEV Community

Cover image for How to Send Alerts from Supabase
Patrik Braborec
Patrik Braborec

Posted on

How to Send Alerts from Supabase

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

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)

Example of Supabase table

I want to receive a Slack message when the following events occur:

  1. A new buyer registers within the last five minutes.
  2. 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>
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up Your profiles.yml

First, create a credentials file:

$ touch profiles.yml
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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: '* * * * *'
Enter fullscreen mode Exit fullscreen mode

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: '* * * * *'
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Alerts Messages

For new users:

new_users:
  slack:
    message: 'You have new users who have registered on the platform! 🚀'
Enter fullscreen mode Exit fullscreen mode

For unapproved rows:

unapproved_users:
  slack:
    message: '📣 We have some unapproved users! Take a look!'
Enter fullscreen mode Exit fullscreen mode

Step 5: Build and Run Emitbase

To run the project, it is necessary to build a Docker image:

$ docker build -t emitbase .
Enter fullscreen mode Exit fullscreen mode

Then, you can just run it and wait for notification on your slack! 🎉

$ docker run -it emitbase
Enter fullscreen mode Exit fullscreen mode

Showcase

The following images show messages in Slack. It's really simple; you can set up your alerts in minutes!

Example of Slack messages from Emitbase

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)