Skip to main content
Version: v1-dev

on().subscribe()

Subscribe to realtime changes in your database.

supabase.channel('*').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: '*', schema: '*'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Notes

  • Realtime is disabled by default for new Projects for better database performance and security. You can turn it on by managing replication.
  • If you want to receive the "previous" data for updates and deletes, you will need to set REPLICA IDENTITY to FULL, like this: ALTER TABLE your_table REPLICA IDENTITY FULL;

Examples

Listen to all database changes

supabase.channel('*').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: '*', schema: '*'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Listening to a specific table

supabase.channel('public:countries').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: '*', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Listening to inserts

supabase.channel('public:countries').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: 'INSERT', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Listening to updates

By default, Supabase will send only the updated record. If you want to receive the previous values as well you can enable full replication for the table you are listening too:

alter table "your_table" replica identity full;
supabase.channel('public:countries').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: 'UPDATE', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Listening to deletes

By default, Supabase does not send deleted records. If you want to receive the deleted record you can enable full replication for the table you are listening too:

alter table "your_table" replica identity full;
supabase.channel('public:countries').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(event: 'DELETE', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
},
).subscribe();

Listening to multiple events

You can chain listeners if you want to listen to multiple events for each table.

supabase.channel('public:countries').on(RealtimeListenTypes.postgresChanges,
ChannelFilter(event: 'INSERT', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
}).on(RealtimeListenTypes.postgresChanges,
ChannelFilter(event: 'DELETE', schema: 'public', table: 'countries'),
(payload, [ref]) {
print('Change received: ${payload.toString()}');
}).subscribe();

Listening to row level changes

You can listen to individual rows using the format {table}:{col}=eq.{val} - where {col} is the column name, and {val} is the value which you want to match.

supabase.channel('public:countries:id=eq.200').on(
RealtimeListenTypes.postgresChanges,
ChannelFilter(
event: 'UPDATE',
schema: 'public',
table: 'countries',
filter: 'id=eq.200',
), (payload, [ref]) {
print('Change received: ${payload.toString()}');
}).subscribe();