Realtime Postgres Changes vs Broadcast

Hey all, I am currently working on a collaborative Google Docs/Notion clone in NextJS as a personal project. I saw the recent video from supabase talking about the new broadcast feature, and it had me wondering whether or not I should use Postgres Changes or Broadcast to ensure that all users of a project page have the most updated information from the database. Apart from Broadcast being more performant and having the ability to filter and add data to the payload delivered to the clients, are there any other major differences between the two features that I should be aware of? Which realtime feature would you use in this situation?
6 Replies
garyaustin
garyaustin7mo ago
I assume you mean a few tables and not the entire database. I'm switching postgres_changes to the broadcast method whenever I get back to working on that part of my own project. The other thing it allows is to deal with losing the connection and not missing data. When you reconnect you can, based on a timestamp of last update) go and load all the missed messages from the last one you got potentially with the message table, but worst case with your own message history table that gets cleaned with cron after a "safe" amount of time.
Charmantle
CharmantleOP7mo ago
Ah yes i mean a couple of tables. Im not quite sure about what you mean about a messages table. In my project I have two realtime tables that clients will receive payloads from. Would those be my “messages” tables in that case?
garyaustin
garyaustin7mo ago
There is a realtime.messages table that the trigger functions shown in the broadcast docs are inserting to.
Charmantle
CharmantleOP7mo ago
Ohh i see now Thanks!
gnollo
gnollo16h ago
Hi, I am trying realtime messages on postgres changes, I see them in Realtime inspector on the web, but my "listener" is not even joining. What am I doing wrong? This is my Python code:
import asyncio
from supabase import acreate_client, AsyncClient

# Initialize the Supabase client
SUPABASE_URL = "https://MYHOST.supabase.co"
SUPABASE_API_KEY = "sb_publishable_MYKEY"

SUPABASE_MATCHES_TABLE = 'matches'

async def create_supabase():
try:
supabase: AsyncClient = await acreate_client(SUPABASE_URL, SUPABASE_API_KEY)
return supabase
except Exception as e:
print(f"SUPABASE error creating client: {e}")


async def listen_postgres_changes():

supabase = await create_supabase()
if not supabase:
print("SUPABASE client not created, exiting...")
return

joined = False
while not joined:
try:
changes = await supabase.channel('db-changes').on_postgres_changes(
"UPDATE", # Listen only to UPDATEs
schema="public",
table="matches",
callback=lambda payload: print(payload)
).subscribe()
joined = changes.is_joined
if joined:
print(f"Listening to changes on Supabase table 'matches'... STATE: {changes.state}")
else:
print(f"Failed to join the channel (state {changes.state}), retrying in 5 seconds...")
await asyncio.sleep(5)
except Exception as e:
print(f"SUPABASE error subscribing to changes: {e}")

if __name__ == "__main__":
loop = asyncio.get_event_loop()
asyncio.run(listen_postgres_changes())
loop.run_forever()
import asyncio
from supabase import acreate_client, AsyncClient

# Initialize the Supabase client
SUPABASE_URL = "https://MYHOST.supabase.co"
SUPABASE_API_KEY = "sb_publishable_MYKEY"

SUPABASE_MATCHES_TABLE = 'matches'

async def create_supabase():
try:
supabase: AsyncClient = await acreate_client(SUPABASE_URL, SUPABASE_API_KEY)
return supabase
except Exception as e:
print(f"SUPABASE error creating client: {e}")


async def listen_postgres_changes():

supabase = await create_supabase()
if not supabase:
print("SUPABASE client not created, exiting...")
return

joined = False
while not joined:
try:
changes = await supabase.channel('db-changes').on_postgres_changes(
"UPDATE", # Listen only to UPDATEs
schema="public",
table="matches",
callback=lambda payload: print(payload)
).subscribe()
joined = changes.is_joined
if joined:
print(f"Listening to changes on Supabase table 'matches'... STATE: {changes.state}")
else:
print(f"Failed to join the channel (state {changes.state}), retrying in 5 seconds...")
await asyncio.sleep(5)
except Exception as e:
print(f"SUPABASE error subscribing to changes: {e}")

if __name__ == "__main__":
loop = asyncio.get_event_loop()
asyncio.run(listen_postgres_changes())
loop.run_forever()
The output:
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
Failed to join the channel (state joining), retrying in 5 seconds...
gnollo
gnollo16h ago
On supabase web I see the listening working
No description

Did you find this page helpful?