Subscribe to channel
Creates an event handler that listens to changes.
- By default, Broadcast and Presence are enabled for all projects.
- By default, listening to database changes is disabled for new projects due to database performance and security concerns. You can turn it on by managing Realtime's replication.
- You can receive the "previous" data for updates and deletes by setting the table's
REPLICA IDENTITY
toFULL
(e.g.,ALTER TABLE your_table REPLICA IDENTITY FULL;
). - Row level security is not applied to delete statements. When RLS is enabled and replica identity is set to full, only the primary key is sent to clients.
Parameters
- typeREQUIRED"broadcast"
One of "broadcast", "presence", or "postgres_changes".
- filterREQUIREDobject
Custom object specific to the Realtime feature detailing which payloads to receive.
eventREQUIREDstring - callbackREQUIREDfunction
Function to be invoked when event handler is triggered.
supabase
.channel('any')
.on('broadcast', { event: 'cursor-pos' }, payload => {
console.log('Cursor position received!', payload)
})
.subscribe((status) => {
if (status === 'SUBSCRIBED') {
channel.send({
type: 'broadcast',
event: 'cursor-pos',
payload: { x: Math.random(), y: Math.random() },
})
}
})