Creates an event handler that listens to changes.
REPLICA IDENTITY
to FULL
(e.g., ALTER TABLE your_table REPLICA IDENTITY FULL;
).const channel = supabase.channel("room1")
channel.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() \},
\});
\}
\});
const channel = supabase.channel('room1')
channel
.on('presence', \{ event: 'sync' \}, () => \{
console.log('Synced presence state: ', channel.presenceState())
\})
.subscribe(async (status) => \{
if (status === 'SUBSCRIBED') \{
await channel.track(\{ online_at: new Date().toISOString() \})
\}
\})
const channel = supabase.channel('room1')
channel
.on('presence', \{ event: 'join' \}, (\{ newPresences \}) => \{
console.log('Newly joined presences: ', newPresences)
\})
.subscribe(async (status) => \{
if (status === 'SUBSCRIBED') \{
await channel.track(\{ online_at: new Date().toISOString() \})
\}
\})
const channel = supabase.channel('room1')
channel
.on('presence', \{ event: 'leave' \}, (\{ leftPresences \}) => \{
console.log('Newly left presences: ', leftPresences)
\})
.subscribe(async (status) => \{
if (status === 'SUBSCRIBED') \{
await channel.track(\{ online_at: new Date().toISOString() \})
await channel.untrack()
\}
\})
supabase
.channel('room1')
.on('postgres_changes', \{ event: '*', schema: '*' \}, payload => \{
console.log('Change received!', payload)
\})
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: '*', schema: 'public', table: 'countries' \}, payload => \{
console.log('Change received!', payload)
\})
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: 'INSERT', schema: 'public', table: 'countries' \}, payload => \{
console.log('Change received!', payload)
\})
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: 'UPDATE', schema: 'public', table: 'countries' \}, payload => \{
console.log('Change received!', payload)
\})
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: 'DELETE', schema: 'public', table: 'countries' \}, payload => \{
console.log('Change received!', payload)
\})
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: 'INSERT', schema: 'public', table: 'countries' \}, handleRecordInserted)
.on('postgres_changes', \{ event: 'DELETE', schema: 'public', table: 'countries' \}, handleRecordDeleted)
.subscribe()
supabase
.channel('room1')
.on('postgres_changes', \{ event: 'UPDATE', schema: 'public', table: 'countries', filter: 'id=eq.200' \}, handleRecordUpdated)
.subscribe()