• Stars
    star
    6,756
  • Rank 5,846 (Top 0.2 %)
  • Language
    Elixir
  • License
    Apache License 2.0
  • Created about 5 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Broadcast, Presence, and Postgres Changes via WebSockets

Supabase Logo

Supabase Realtime

Send ephemeral messages, track and synchronize shared state, and listen to Postgres changes all over WebSockets.
Multiplayer Demo ยท Request Feature ยท Report Bug

Status

Features v1 v2 Status
Postgres Changes โœ” โœ” GA
Broadcast โœ” Beta
Presence โœ” Beta

This repository focuses on version 2 but you can still access the previous version's code and Docker image. For the latest Docker images go to https://hub.docker.com/r/supabase/realtime.

The codebase is under heavy development and the documentation is constantly evolving. Give it a try and let us know what you think by creating an issue. Watch releases of this repo to get notified of updates. And give us a star if you like it!

Overview

What is this?

This is a server built with Elixir using the Phoenix Framework that enables the following functionality:

  • Broadcast: Send ephemeral messages from client to clients with low latency.
  • Presence: Track and synchronize shared state between clients.
  • Postgres Changes: Listen to Postgres database changes and send them to authorized clients.

For a more detailed overview head over to Realtime guides.

Does this server guarantee message delivery?

The server does not guarantee that every message will be delivered to your clients so keep that in mind as you're using Realtime.

Quick start

You can check out the Multiplayer demo that features Broadcast, Presence and Postgres Changes under the demo directory: https://github.com/supabase/realtime/tree/main/demo.

Client libraries

Server Setup

To get started, spin up your Postgres database and Realtime server containers defined in docker-compose.yml. As an example, you may run docker-compose -f docker-compose.yml up.

Note Supabase runs Realtime in production with a separate database that keeps track of all tenants. However, a schema, _realtime, is created when spinning up containers via docker-compose.yml to simplify local development.

A tenant has already been added on your behalf. You can confirm this by checking the _realtime.tenants and _realtime.extensions tables inside the database.

You can add your own by making a POST request to the server. You must change both name and external_id while you may update other values as you see fit:

  curl -X POST \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIiLCJpYXQiOjE2NzEyMzc4NzMsImV4cCI6MTcwMjc3Mzk5MywiYXVkIjoiIiwic3ViIjoiIn0._ARixa2KFUVsKBf3UGR90qKLCpGjxhKcXY4akVbmeNQ' \
  -d $'{
    "tenant" : {
      "name": "realtime-dev",
      "external_id": "realtime-dev",
      "jwt_secret": "a1d99c8b-91b6-47b2-8f3c-aa7d9a9ad20f",
      "extensions": [
        {
          "type": "postgres_cdc_rls",
          "settings": {
            "db_name": "postgres",
            "db_host": "host.docker.internal",
            "db_user": "postgres",
            "db_password": "postgres",
            "db_port": "5432",
            "region": "us-west-1",
            "poll_interval_ms": 100,
            "poll_max_record_bytes": 1048576,
          }
        }
      ]
    }
  }' \
  http://localhost:4000/api/tenants

Note The Authorization token is signed with the secret set by API_JWT_SECRET in docker-compose.yml.

If you want to listen to Postgres changes, you can create a table and then add the table to the supabase_realtime publication:

create table test (
  id serial primary key
);

alter publication supabase_realtime add table test;

You can start playing around with Broadcast, Presence, and Postgres Changes features either with the client libs (e.g. @supabase/realtime-js), or use the built in Realtime Inspector on localhost, http://localhost:4000/inspector/new (make sure the port is correct for your development environment).

The WebSocket URL must contain the subdomain, external_id of the tenant on the _realtime.tenants table, and the token must be signed with the jwt_secret that was inserted along with the tenant.

If you're using the default tenant, the URL is ws://realtime-dev.localhost:4000/socket (make sure the port is correct for your development environment), and you can use eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MDMwMjgwODcsInJvbGUiOiJwb3N0Z3JlcyJ9.tz_XJ89gd6bN8MBpCl7afvPrZiBH6RB65iA1FadPT3Y for the token. The token must have exp and role (database role) keys.

ALL RELEVANT OPTIONS

Note Realtime server is tightly coupled to Fly.io at the moment.

PORT                       # {number}      Port which you can connect your client/listeners
DB_HOST                    # {string}      Database host URL
DB_PORT                    # {number}      Database port
DB_USER                    # {string}      Database user
DB_PASSWORD                # {string}      Database password
DB_NAME                    # {string}      Postgres database name
DB_ENC_KEY                 # {string}      Key used to encrypt sensitive fields in _realtime.tenants and _realtime.extensions tables. Recommended: 16 characters.
DB_AFTER_CONNECT_QUERY     # {string}      Query that is run after server connects to database.
API_JWT_SECRET             # {string}      Secret that is used to sign tokens used to manage tenants and their extensions via HTTP requests.
FLY_ALLOC_ID               # {string}      This is auto-set when deploying to Fly. Otherwise, set to any string.
FLY_APP_NAME               # {string}      A name of the server.
FLY_REGION                 # {string}        Name of the region that the server is running in. Fly auto-sets this on deployment. Otherwise, set to any string.
SECRET_KEY_BASE            # {string}      Secret used by the server to sign cookies. Recommended: 64 characters.
ERL_AFLAGS                 # {string}      Set to either "-proto_dist inet_tcp" or "-proto_dist inet6_tcp" depending on whether or not your network uses IPv4 or IPv6, respectively.
ENABLE_TAILSCALE           # {string}      Use Tailscale for private networking. Set to either 'true' or 'false'.
TAILSCALE_APP_NAME         # {string}      Name of the Tailscale app.
TAILSCALE_AUTHKEY          # {string}      Auth key for the Tailscape app.
DNS_NODES                  # {string}      Node name used when running server in a cluster.
MAX_CONNECTIONS            # {string}     Set the soft maximum for WebSocket connections. Defaults to '16384'.
MAX_HEADER_LENGTH          # {string}      Set the maximum header length for connections (in bytes). Defaults to '4096'.
NUM_ACCEPTORS              # {string}     Set the number of server processes that will relay incoming WebSocket connection requests. Defaults to '100'.
DB_QUEUE_TARGET            # {string}     Maximum time to wait for a connection from the pool. Defaults to '5000' or 5 seconds. See for more info: https://hexdocs.pm/db_connection/DBConnection.html#start_link/2-queue-config.
DB_QUEUE_INTERVAL          # {string}     Interval to wait to check if all connections were checked out under DB_QUEUE_TARGET. If all connections surpassed the target during this interval than the target is doubled. Defaults to '5000' or 5 seconds. See for more info: https://hexdocs.pm/db_connection/DBConnection.html#start_link/2-queue-config.
DB_POOL_SIZE               # {string}     Sets the number of connections in the database pool. Defaults to '5'.
SLOT_NAME_SUFFIX           # {string}     This is appended to the replication slot which allows making a custom slot name. May contain lowercase letters, numbers, and the underscore character. Together with the default `supabase_realtime_replication_slot`, slot name should be up to 64 characters long.

WebSocket URL

The WebSocket URL is in the following format for local development: ws://[external_id].localhost:4000/socket/websocket

If you're using Supabase's hosted Realtime in production the URL is wss://[project-ref].supabase.co/realtime/v1/websocket?apikey=[anon-token]&log_level=info&vsn=1.0.0"

WebSocket Connection Authorization

WebSocket connections are authorized via symmetric JWT verification. Only supports JWTs signed with the following algorithms:

  • HS256
  • HS384
  • HS512

Verify JWT claims by setting JWT_CLAIM_VALIDATORS:

e.g. {'iss': 'Issuer', 'nbf': 1610078130}

Then JWT's "iss" value must equal "Issuer" and "nbf" value must equal 1610078130.

Note:

JWT expiration is checked automatically. exp and role (database role) keys are mandatory.

Authorizing Client Connection: You can pass in the JWT by following the instructions under the Realtime client lib. For example, refer to the Usage section in the @supabase/realtime-js client library.

License

This repo is licensed under Apache 2.0.

Credits

More Repositories

1

supabase

The open source Firebase alternative. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript
72,511
star
2

supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
TypeScript
3,211
star
3

pg_graphql

GraphQL support for PostgreSQL
Rust
2,888
star
4

supavisor

A cloud-native, multi-tenant Postgres connection pooler.
Elixir
1,731
star
5

supabase-py

Python Client for Supabase. Query Postgres from Flask, Django, FastAPI. Python user authentication, security policies, edge functions, file storage, and realtime data streaming. Good first issue.
Python
1,680
star
6

index_advisor

PostgreSQL Index Advisor
PLpgSQL
1,612
star
7

ui

Supabase UI Library
TypeScript
1,563
star
8

auth

A JWT based API for managing users and issuing JWT tokens
Go
1,484
star
9

postgres

Unmodified Postgres with some useful plugins
Shell
1,366
star
10

cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
Go
1,042
star
11

postgrest-js

Isomorphic JavaScript client for PostgREST.
TypeScript
1,036
star
12

pg_jsonschema

PostgreSQL extension providing JSON Schema validation
Rust
1,008
star
13

postgres-meta

A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries
TypeScript
912
star
14

auth-helpers

A collection of framework specific Auth utilities for working with Supabase.
TypeScript
905
star
15

storage

S3 compatible object storage service that stores metadata in Postgres
TypeScript
776
star
16

supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
Dart
719
star
17

supabase-swift

A Swift client for Supabase
Swift
696
star
18

edge-runtime

A server based on Deno runtime, capable of running JavaScript, TypeScript, and WASM services.
Rust
671
star
19

supa_audit

Generic Table Auditing
PLpgSQL
647
star
20

pg_replicate

Build Postgres replication apps in Rust
Rust
564
star
21

wrappers

Postgres Foreign Data Wrapper development framework in Rust.
Rust
549
star
22

stripe-sync-engine

Sync your Stripe account to you Postgres database.
TypeScript
490
star
23

auth-ui

Pre-built Auth UI for React
TypeScript
405
star
24

supabase-dart

A Dart client for Supabase
Dart
399
star
25

pg_crdt

POC CRDT support in Postgres
Rust
395
star
26

dbdev

Database Package Registry for Postgres
PLpgSQL
368
star
27

auth-js

An isomorphic Javascript library for Supabase Auth.
CSS
357
star
28

realtime-js

An isomorphic Javascript client for Supabase Realtime server.
TypeScript
318
star
29

examples-archive

Supabase Examples Archive
TypeScript
287
star
30

pg_netstat

PostgreSQL extension to monitor database network traffic
Rust
247
star
31

postgrest-py

PostgREST client for Python. This library provides an ORM interface to PostgREST
Python
230
star
32

pg_net

A PostgreSQL extension that enables asynchronous (non-blocking) HTTP/HTTPS requests with SQL
C
223
star
33

vecs

Postgres/pgvector Python Client
Python
219
star
34

grid

A react component to display your Postgresql table data. Used in Supabase Dashboard app.
TypeScript
202
star
35

libcluster_postgres

Postgres strategy for libcluster
Elixir
190
star
36

supabase-grafana

Observability for your Supabase project, using Prometheus/Grafana
Shell
187
star
37

vault

Extension for storing encrypted secrets in the Vault
PLpgSQL
174
star
38

headless-vector-search

Supabase Toolkit to perform vector similarity search on your knowledge base embeddings.
TypeScript
155
star
39

workflows

Elixir
139
star
40

postgrest-dart

Dart client for PostgREST
Dart
136
star
41

realtime-py

A Python Client for Phoenix Channels
Python
130
star
42

storage-js

JS Client library to interact with Supabase Storage
TypeScript
129
star
43

walrus

Applying RLS to PostgreSQL WAL
PLpgSQL
119
star
44

setup-cli

A GitHub action for interacting with your Supabase projects using the CLI.
TypeScript
113
star
45

postgres-deno

A PostgreSQL extension for Deno: run Typescript in PostgreSQL functions and triggers.
107
star
46

embeddings-generator

GitHub Action to generate embeddings from the markdown files in your repository.
TypeScript
87
star
47

realtime-dart

A dart client for Supabase Realtime server.
Dart
84
star
48

splinter

Supabase Postgres Linter: Performance and Security Advisors
PLpgSQL
83
star
49

repository.surf

๐Ÿ„
JavaScript
80
star
50

supabase-ui-web

TypeScript
74
star
51

auth-py

Python client implementation for Supabase Auth
Python
73
star
52

self-hosted-edge-functions-demo

A demo of how to self-host Supabase Edge Functions on Fly.io
TypeScript
72
star
53

ssr

Supabase clients for use in server-side rendering frameworks.
TypeScript
65
star
54

functions-js

TypeScript
62
star
55

supabase-action-example

TypeScript
61
star
56

supautils

PostgreSQL extension that secures a cluster on a cloud environment
C
59
star
57

supabase-admin-api

API to administer the Supabase server (KPS)
Go
51
star
58

nix-postgres

Experimental port of supabase/postgres to Nix
Nix
47
star
59

gotrue-dart

A dart client library for GoTrue.
Dart
46
star
60

benchmarks

SCSS
45
star
61

storage-py

Python
42
star
62

grafana-agent-fly-example

Deploy a Grafana Agent on Fly to scrape Prometheus metrics from Supabase and send them to Grafana Cloud
Shell
36
star
63

functions-relay

API Gateway for Supabase Edge functions
TypeScript
35
star
64

benchmarks-archive

Infrastucture benchmarks
Nix
31
star
65

hibp

Go library for HaveIBeenPwned.org's pwned passwords API.
Go
31
star
66

supabase.ai

iykyk
HTML
27
star
67

terraform-provider-supabase

Go
23
star
68

livebooks

A collection of Elixir Livebooks for Supabase
Dockerfile
21
star
69

storage-dart

Dart client library to interact with Supabase Storage
Dart
21
star
70

orb-sync-engine

TypeScript
12
star
71

functions-py

Python
12
star
72

auth-elements

Components to add Supabase Auth to any application
TypeScript
11
star
73

rfcs

11
star
74

.github

Org-wide default community health files & templates.
10
star
75

scoop-bucket

8
star
76

functions-dart

Dart
8
star
77

test-reports

Repository to store test reports data and host reporting in gh-pages
7
star
78

plug_caisson

An Elixir Plug library for handling compressed requests
Elixir
7
star
79

flyswatter

Deploy a global pinger on Fly
Elixir
6
star
80

tests

TypeScript
4
star
81

mailme

A clone of Netlify's mailme package used in Supabase Auth / GoTrue.
Go
4
star
82

pgextkit

Rust
3
star
83

homebrew-tap

Ruby
3
star
84

fly-preview

TypeScript
3
star
85

shared-types

TypeScript
3
star
86

supa_type

The Missing PostgreSQL Data Types
Nix
3
star
87

test-inspector

Check your test results against the reference run and compare coverage for multiple client libraries
Go
2
star
88

productions

Supabase SynthWave. The best soundtrack to build an app in a weekend and scale to billions.
TypeScript
2
star
89

design-tokens

1
star