supabase / realtime
- среда, 3 июня 2020 г. в 00:21:40
Elixir
Listen to your to PostgreSQL database in realtime via websockets. Built with Elixir.
Listens to changes in a PostgreSQL Database and broadcasts them over websockets.
Contents
This repo is still under heavy development and the documentation is evolving. You're welcome to try it, but expect some breaking changes. Watch "releases" of this repo to receive a notifification when we are ready for Beta. And give us a star if you like it!
import { Socket } = '@supabase/realtime-js'
var socket = new Socket(process.env.REALTIME_URL)
socket.connect()
// Listen to only INSERTS on the 'users' table in the 'public' schema
var allChanges = this.socket.channel('realtime:public:users')
.join()
.on('INSERT', payload => { console.log('Update received!', payload) })
// Listen to all changes from the 'public' schema
var allChanges = this.socket.channel('realtime:public')
.join()
.on('*', payload => { console.log('Update received!', payload) })
// Listen to all changes in the database
let allChanges = this.socket.channel('realtime:*')
.join()
.on('*', payload => { console.log('Update received!', payload) })
This is an Elixir server (Phoenix) that allows you to listen to changes in your database via websockets.
It works like this:
NOTIFY
?A few reasons:
If you just want to start it up and see it in action:
docker-compose up
http://localhost:3000
(be patient, node_modules will need to install)Install the client library
npm install --save @supabase/realtime-js
Set up the socket
import { Socket } = '@supabase/realtime-js'
const REALTIME_URL = process.env.REALTIME_URL || 'http://localhost:4000'
var socket = new Socket(REALTIME_URL)
socket.connect()
You can listen to these events on each table:
const EVENTS = {
EVERYTHING: '*',
INSERT: 'INSERT',
UPDATE: 'UPDATE',
DELETE: 'DELETE'
}
Example 1: Listen to all INSERTS, on your users
table
var allChanges = this.socket.channel('realtime:public:users')
.join()
.on(EVENTS.INSERT, payload => { console.log('Record inserted!', payload) })
Example 2: Listen to all UPDATES in the public
schema
var allChanges = this.socket.channel('realtime:public')
.join()
.on(EVENTS.UPDATE, payload => { console.log('Update received!', payload) })
Example 3: Listen to all INSERTS, UPDATES, and DELETES, in all schemas
let allChanges = this.socket.channel('realtime:*')
.join()
.on(EVENTS.EVERYTHING, payload => { console.log('Update received!', payload) })
There are a some requirements for your database
wal_level
set 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;
max_replication_slots
to at least 1: ALTER SYSTEM SET max_replication_slots = 5;
PUBLICATION
for this server to listen to: CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
REPLICA IDENTITY
to FULL
like this: ALTER TABLE your_table REPLICA IDENTITY FULL;
. This has to be set for each table unfortunately.The easiest way to get started is just to use our docker image. We will add more deployment methods soon.
# Update the environment variables to point to your own database
docker run \
-e DB_HOST='docker.for.mac.host.internal' \
-e DB_NAME='postgres' \
-e DB_USER='postgres' \
-e DB_PASSWORD='postgres' \
-e DB_PORT=5432 \
-e PORT=4000 \
-e HOSTNAME='localhost' \
-e SECRET_KEY_BASE='SOMETHING_SUPER_SECRET' \
-p 4000:4000 \
supabase/realtime
to trigger a release you must tag the commit, then push to origin
git tag -a 7.x.x -m "some stuff about the release"
git push origin 7.x.x
This repo is liscenced under Apache 2.0.