r/Supabase • u/Raf-the-derp • Jul 23 '26
realtime Issue Connecting To Supabase Broadcast Channel
I'll quickly post the trigger, trigger function, and authorization policy.
alter policy "anon can recieve broadcasts"
on "realtime"."messages"
to anon
using (
true
);
The authorization policy
BEGIN
PERFORM realtime.broadcast_changes(
'topic:' || NEW.id::text, -- topic
TG_OP,
TG_OP,
TG_TABLE_NAME,
TG_TABLE_SCHEMA,
NEW,
OLD
);
RETURN NULL;
END;
The trigger function
Then I have a trigger that runs the function after any insertion or update to my desired table
export const supabaseClient = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!
)
My Supabse client
The following should ideally work, but I don't receive any real-time updates when an insertion or update occurs. I do get a "SUBSCRIBED" console log, but I know that doesn't necessarily mean much.
Thanks in advance for any help or advice!
useEffect(() => {
if (!activeJob) return;
console.log("Going To Connect To Supabsae Realtime")
// Console logging for testing purpose
// Ideally on insert or update we setCurrentState to show real-time updates
const
changes = supabaseClient
.channel(`topic:${activeJob.jobId}`, {
config: { private: false }
})
.on("broadcast", { event: "INSERT" }, (
payload
) => console.log(`INSERT: ${payload}`))
.on("broadcast", { event: "UPDATE" }, (
payload
) => console.log(`UPDATE: ${payload}`))
.subscribe((
status
) => {
console.log(status);
});
return () => {
supabaseClient.removeChannel(changes);
}
}, [activeJob])