Error trying to connect to postgres_changes in realtime
Hello I'm facing a problem here related to realtime database.
I have a table called documents, I enabled RLS for everyone can select and also enabled realtime, but i try to connect to it many times in my code and it doesn't work, the status keep saying: CLOSED, CHANNEL_ERROR
I tried changing to "broadcast" and it subscribed, only in postgres_changes it fails.

16 Replies
So you close the channel in the useEffect?
If so and you use the same channel name for two different pages as one is closing the other is opening and they step on each other.
At least that is one situation I've seen for the Closing of the channel with useEffect involved.
no
let me show it all to be better for u
hm...
so its better to use it outside?
What is the purpose of the broadcast?
the broadcast is
when i create a doc i can listen to this event
No. Broadcast is not the same as Postgres changes.
oh sorry so let me change it
at first i connected with .on("broadbast")
that was alright no error
There are two ways to do this.
One is postgres_changes. You set those up on the tables with publications (a tab in the dashboard) an have to meet RLS.
then i changed to .on("postgres_changes") and thats how it's now with error
The other method that can be used is database broadcast where you use a private broadcast channel and a trigger function on the table to send table information.
understand
not much actually the trigger function
do you have a suggestion for helping me
like in the code etc
let me search about this trigger you said there
You have to pick a method. Postgres_changes is "easier". It is documented in the realtime docs as that. It requires realtime enabled on the table and that you meet RLS. It is slower than the next method. https://supabase.com/docs/guides/realtime/postgres-changes
The other is is Broadcast with a trigger https://supabase.com/docs/guides/realtime/subscribing-to-database-changes#using-broadcast
yes
now i understand well
So I should try this one
and forget the postgres_changes bc of the error
If I were doing a new app I would learn the Broadcast messages with the database trigger method as it is faster and more flexible. But it is a bit harder to learn and get going.
yeah
but anyways thanks a lot
I don't know why your postgres_changes above was closing, but you can't do broadcast on that same channel.
ook
thank you