Now you can use Realtime Broadcast to scale database changes sent to clients with Broadcast from Database.

You can use Supabase Realtime build immersive features like notifications, chats, live cursors, shared whiteboards, multiplayer games, and listen to Database changes.

Realtime includes the following features:

  • Broadcast, to send low-latency messages using client libraries, REST, or your Database
  • Presence, to store and synchronize online user state consistently across clients
  • Postgres Changes, polls the Database, listens for changes, and sends messages to clients

Broadcasting from the Database is our latest improvement. It requires more initial setup than Postgres Changes, but offers more benefits:

  • You can target specific actions (INSERT, UPDATE, DELETE, TRUNCATE)
  • Choose which columns to send in the body of the message instead of the full record
  • Use SQL to selectively send data to specific channels

You now have two options for building real-time applications using database changes:

  • Broadcast from Database, to send messages triggered by changes within the Database itself
  • Postgres Changes, polling Database for changes

There are several scenarios where you will want to use Broadcast from Database instead of Postgres Changes, including:

  • Applications with many connected users
  • Sanitizing the payload of a message instead of providing the full record
  • Reduction in latency of sent messages

Let’s walk through how to set up Broadcast from Database.

First, set up Row-Level Security (RLS) policies to control user access to relevant messages:


_10

create policy "Authenticated users can receive broadcasts"

_10

on "realtime"."messages"

_10

for select

_10

to authenticated

_10

using ( true );


Then, set up the function that will be called whenever a Database change is detected:


_18

create or replace function public.your_table_changes()

_18

returns trigger

_18

security definer

_18

language plpgsql

_18

as $$

_18

begin

_18

perform realtime.broadcast_changes(

_18

'topic:' || coalesce(NEW.topic, OLD.topic) ::text, -- topic - the topic to which we're broadcasting

_18

TG_OP, -- event - the event that triggered the function

_18

TG_OP, -- operation - the operation that triggered the function

_18

TG_TABLE_NAME, -- table - the table that caused the trigger

_18

TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger

_18

NEW, -- new record - the record after the change

_18

OLD -- old record - the record before the change

_18

);

_18

return null;

_18

end;

_18

$$;


Then, set up the trigger conditions under which you will execute the function:


_10

create trigger broadcast_changes_for_your_table_trigger

_10

after insert or update or delete

_10

on public.your_table

_10

for each row

_10

execute function your_table_changes();


And finally, set up your client code to listen for changes:


_10

const id = 'id'

_10

await supabase.realtime.setAuth() // Needed for Realtime Authorization

_10

const changes = supabase

_10

.channel(`topic:${id}`, {

_10

config: { private: true },

_10

})

_10

.on('broadcast', { event: 'INSERT' }, (payload) => console.log(payload))

_10

.on('broadcast', { event: 'UPDATE' }, (payload) => console.log(payload))

_10

.on('broadcast', { event: 'DELETE' }, (payload) => console.log(payload))

_10

.subscribe()


Be sure to read the docs for more information and example use cases.

Realtime Broadcast from Database sets up a replication slot against a publication created for the realtime.messages table. This lets Realtime listen for Write Ahead Log (WAL) changes whenever new rows are inserted.

When Realtime spots a new insert in the WAL, it broadcasts that message to the target channel right away.

We created two helper functions:

  • realtime.send: A simple function that adds messages to the realtime.messages table
  • realtime.broadcast_changes: A more advanced function that creates payloads similar to Postgres Changes

The realtime.send function is designed to work safely inside triggers. It catches exceptions and uses pg_notify to send error information to the Realtime server for proper logging. This keeps your triggers from breaking if something goes wrong.

These improvements let us scale subscribing to database changes to tens of thousands of connected users at once. They also enable new uses like:

  1. Broadcasting directly from Database functions
  2. Sending only specific fields to connected clients
  3. Creating scheduled events using Supabase Cron

All this makes your real-time applications faster and more flexible.

Supabase Realtime can help you build more compelling experiences for your applications.