/realtime

Primary LanguageElixirApache License 2.0Apache-2.0

Supabase Realtime

Status: Unfit for production

This repo hasn't yet implemented Socket Authentication so it is not recommended for use in production

Listens to changes in a PostgreSQL Database and broadcasts them over websockets. This is the realtime server for usage with your development machine.

Usage

DB requirements:

  • Replication wal_level must be logical
  • Must be at least one free slot max_replication_slots
  • must create a publication called supabase_realtime @todo: auto create
    • CREATE PUBLICATION supabase_realtime FOR ALL TABLES;
  • This will set up a new slot called supabase_realtime_slot if it doesn't already exist
    • CREATE_REPLICATION_SLOT 'supabase_realtime_slot' LOGICAL pgoutput NOEXPORT_SNAPSHOT;
  • If you want to receive the "old" data on updates you need to set replica identity to "FULL". eg:
    • alter table YOUR_TABLE_NAME replica identity full;

Works without wal2json :)

Client

See https://github.com/supabase/realtime-js for details.

Install

npm install @supabase/realtime-js

Use

import { Socket } = '@supabase/realtime-js'

var API_SOCKET = process.env.SOCKET_URL
var socket = new Socket(API_SOCKET)
var realtimeChannel = this.socket.channel('realtime')

socket.connect()
if (realtimeChannel.state !== 'joined') {
  realtimeChannel
    .join()
    .receive('ok', resp => console.log('Joined successfully', resp))
    .receive('error', resp => console.log('Unable to join', resp))
    .receive('timeout', () => console.log('Networking issue. Still waiting...'))

  // Listen to all changes in the database
  realtimeChannel.on('shout', payload => {
    console.log('Update received!', payload)
  })
}

Server

Docker

The easiest way to use this is to set up a docker compose:

# docker-compose.yml
version: '3'
services:
  realtime:
    image: supabase/realtime
    ports:
      - "4000:4000"
    environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgres
    - POSTGRES_DB=postgres
    - POSTGRES_HOST=localhost
    - POSTGRES_PORT=5432

Then run:

docker-compose up     # Run in foreground on port 4000
docker-compose up -d  # Run in background on port 4000
docker-compose down   # Tear down

Development

Prerequisites

  • Install Docker
  • Install Elixir and Phoenix

Then run:

docker-compose up   # start the database (on port 6543 to avoid PG conflicts)
mix phx.server      # start the elixir app

Format code

mix format mix.exs “lib/**/*.{ex,exs}” “test/**/*.{ex,exs}”

Releases

  • Docker - Builds directly from Github

Credits