S
Supabase2mo ago
Mikey

Supabase broadcast not working in local development

I am using sveltekit and supabase ssr i have a code snippet that proves it is not working.
onMount(() => {
if (!page.data.supabase) return;

const broadcastChannel = page.data.supabase
.channel('TEST')
.on('broadcast', { event: 'typing' }, (payload) => {
//this doesnt log anything from my sendTyping function
console.log(payload);
})
.subscribe();

return () => broadcastChannel.unsubscribe();
});

const sendTyping = () => {
if (!page.data.supabase) return;

const sendingTyping = page.data.supabase.channel('TEST');

sendingTyping
.send({ type: 'broadcast', event: 'typing', payload: { from: page.data.user?.id } })
.then((x) => {
//this tells me that it sent successfully it logs 'typing sent ok'
console.log('typing sent', x);
});
};
onMount(() => {
if (!page.data.supabase) return;

const broadcastChannel = page.data.supabase
.channel('TEST')
.on('broadcast', { event: 'typing' }, (payload) => {
//this doesnt log anything from my sendTyping function
console.log(payload);
})
.subscribe();

return () => broadcastChannel.unsubscribe();
});

const sendTyping = () => {
if (!page.data.supabase) return;

const sendingTyping = page.data.supabase.channel('TEST');

sendingTyping
.send({ type: 'broadcast', event: 'typing', payload: { from: page.data.user?.id } })
.then((x) => {
//this tells me that it sent successfully it logs 'typing sent ok'
console.log('typing sent', x);
});
};
7 Replies
Mikey
MikeyOP2mo ago
CLI version 2.39.2
silentworks
silentworks2mo ago
Did you enable publication on that table? What error are you getting?
Mikey
MikeyOP2mo ago
no error sir, and what table should i target? this is broadcast not postgress changes.
Mikey
MikeyOP2mo ago
I am following these docs, and as I understand it, there are no tables involved. This creates a channel that can be broadcast via an event. https://supabase.com/docs/guides/realtime/broadcast
Broadcast | Supabase Docs
Send low-latency messages using the client libs, REST, or your Database.
Mikey
MikeyOP2mo ago
Should i chain the broadcast with posgress changes? this one works
const messageChannel = page.data.supabase
.channel(`messages-${messageAppState.inboxId}`)
.on(
'postgres_changes',
{ event: '*', schema: 'public', table: 'messages' },
async (payload) => {
messages?.push(payload.new as (typeof messages)[number]);
msgCount++;
}
)
.on('broadcast', { event: 'is_typing' }, (payload) => {
console.log(payload);
})
.subscribe();
const messageChannel = page.data.supabase
.channel(`messages-${messageAppState.inboxId}`)
.on(
'postgres_changes',
{ event: '*', schema: 'public', table: 'messages' },
async (payload) => {
messages?.push(payload.new as (typeof messages)[number]);
msgCount++;
}
)
.on('broadcast', { event: 'is_typing' }, (payload) => {
console.log(payload);
})
.subscribe();
garyaustin
garyaustin2mo ago
You should test with the realtime tab in the dashboard to see if you get the messages you expect. Postgres_changes should not be enabled or used if you are just doing broadcast.
Mikey
MikeyOP2mo ago
Thanks sir gary for help

Did you find this page helpful?