Supabase Realtime is a Change Data Capture server. It allows you to listen to Postgres changes.
If you want to host your own Realtime server, you have options to host using Docker, AWS, DigitalOcean, or build from source.
What is this?
This is an Elixir server (Phoenix) that allows you to listen to changes in your database via websockets.
It works like this:
- the Phoenix server listens to PostgreSQL's replication functionality (using Postgres' logical decoding)
- it converts the byte stream into JSON
- it then broadcasts over websockets.
Cool, but why not just use Postgres'
A few reasons:
- You don't have to set up triggers on every table
- NOTIFY has a payload limit of 8000 bytes and will fail for anything larger. The usual solution is to send an ID then fetch the record, but that's heavy on the database
- This server consumes one connection to the database, then you can connect many clients to this server. Easier on your database, and to scale up you just add realtime servers
What are the benefits?
- The beauty of listening to the replication functionality is that you can make changes to your database from anywhere - your API, directly in the DB, via a console etc - and you will still receive the changes via websockets.
- Decoupling. For example, if you want to send a new slack message every time someone makes a new purchase you might build that functionality directly into your API. This allows you to decouple your async functionality from your API.
- This is built with Phoenix, an extremely scalable Elixir framework
We have set up some simple examples that show how to use this server:
Database set up
There are a some requirements for your database
- It must be Postgres 10+ as it uses logical replication
- Set up your DB for replication
- it must have the
wal_levelset to logical. You can check this by running
SHOW wal_level;. To set the
wal_level, you can call
ALTER SYSTEM SET wal_level = logical;
- You must set
max_replication_slotsto at least 1:
ALTER SYSTEM SET max_replication_slots = 5;
- it must have the
- Create a
PUBLICATIONfor this server to listen to:
CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
- [OPTIONAL] If you want to receive the old record (previous values) on UPDATE and DELETE, you can set the
ALTER TABLE your_table REPLICA IDENTITY FULL;. This has to be set for each table unfortunately.
Server set up
The easiest way to get started is just to use our docker image. We will add more deployment methods soon.
- Fork the repo on GitHub
- Clone the project to your own machine
- Commit changes to your own branch
- Push your work back up to your fork
- Submit a Pull request so that we can review your changes and merge
- Make a commit to bump the version in
- Tag the commit
To trigger a release you must tag the commit, then push to origin.
This repo is licensed under Apache 2.0.