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.
No description
16 Replies
garyaustin
garyaustin•4w ago
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.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
no let me show it all to be better for u
useEffect(() => {
read().then(data => {
if (data) setDocuments(data);
})
const channel = supabase.channel('documents')
channel.on(
"postgres_changes",
{ event: "*", schema: "public", table: "documents" },
(payload) => {
console.log("INSERT received:", payload);
}
)
.subscribe((status) => {
console.log("Status:", status);
});

setTimeout(() => {
channel.send({
type: "broadcast",
event: "ping",
payload: { msg: "Hello from client" },
});
}, 2000);
}, [])
useEffect(() => {
read().then(data => {
if (data) setDocuments(data);
})
const channel = supabase.channel('documents')
channel.on(
"postgres_changes",
{ event: "*", schema: "public", table: "documents" },
(payload) => {
console.log("INSERT received:", payload);
}
)
.subscribe((status) => {
console.log("Status:", status);
});

setTimeout(() => {
channel.send({
type: "broadcast",
event: "ping",
payload: { msg: "Hello from client" },
});
}, 2000);
}, [])
hm... so its better to use it outside?
garyaustin
garyaustin•4w ago
What is the purpose of the broadcast?
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
the broadcast is when i create a doc i can listen to this event
garyaustin
garyaustin•4w ago
No. Broadcast is not the same as Postgres changes.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
oh sorry so let me change it at first i connected with .on("broadbast") that was alright no error
garyaustin
garyaustin•4w ago
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.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
then i changed to .on("postgres_changes") and thats how it's now with error
garyaustin
garyaustin•4w ago
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.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
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
garyaustin
garyaustin•4w ago
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
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
yes now i understand well So I should try this one and forget the postgres_changes bc of the error
garyaustin
garyaustin•4w ago
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.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
yeah but anyways thanks a lot
garyaustin
garyaustin•4w ago
I don't know why your postgres_changes above was closing, but you can't do broadcast on that same channel.
SudsierSpacešŸ‘
SudsierSpacešŸ‘OP•4w ago
ook thank you

Did you find this page helpful?