apps/www/_blog/2025-04-02-realtime-broadcast-from-database.mdx
Now you can use Realtime Broadcast to scale database changes sent to clients with Broadcast from Database.
<div className="video-container mb-8"> <iframe className="w-full" src="https://www.youtube-nocookie.com/embed/vRorVm_UbhA" title="Introducing Realtime Broadcast from Database" allow="accelerometer; autoplay; clipboard-write; encrypted-media; fullscreen; gyroscope; picture-in-picture; web-share" allowfullscreen /> </div>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:
Broadcasting from the Database is our latest improvement. It requires more initial setup than Postgres Changes, but offers more benefits:
INSERT, UPDATE, DELETE, TRUNCATE)You now have two options for building real-time applications using database changes:
There are several scenarios where you will want to use Broadcast from Database instead of Postgres Changes, including:
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:
create policy "Authenticated users can receive broadcasts"
on "realtime"."messages"
for select
to authenticated
using ( true );
Then, set up the function that will be called whenever a Database change is detected:
create or replace function public.your_table_changes()
returns trigger
security definer
language plpgsql
as $$
begin
perform realtime.broadcast_changes(
'topic:' || coalesce(NEW.topic, OLD.topic) ::text, -- topic - the topic to which we're broadcasting
TG_OP, -- event - the event that triggered the function
TG_OP, -- operation - the operation that triggered the function
TG_TABLE_NAME, -- table - the table that caused the trigger
TG_TABLE_SCHEMA, -- schema - the schema of the table that caused the trigger
NEW, -- new record - the record after the change
OLD -- old record - the record before the change
);
return null;
end;
$$;
Then, set up the trigger conditions under which you will execute the function:
create trigger broadcast_changes_for_your_table_trigger
after insert or update or delete
on public.your_table
for each row
execute function your_table_changes();
And finally, set up your client code to listen for changes:
const id = 'id'
await supabase.realtime.setAuth() // Needed for Realtime Authorization
const changes = supabase
.channel(`topic:${id}`, {
config: { private: true },
})
.on('broadcast', { event: 'INSERT' }, (payload) => console.log(payload))
.on('broadcast', { event: 'UPDATE' }, (payload) => console.log(payload))
.on('broadcast', { event: 'DELETE' }, (payload) => console.log(payload))
.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 tablerealtime.broadcast_changes: A more advanced function that creates payloads similar to Postgres ChangesThe 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:
All this makes your real-time applications faster and more flexible.
Supabase Realtime can help you build more compelling experiences for your applications.