Flutter: Subscribe to channel

Subscribe to realtime changes in your database.

Examples

Listen to database changes

supabase
    .channel('public:countries')
    .onPostgresChanges(
        event: PostgresChangeEvent.all,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Change received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to inserts

supabase
    .channel('public:countries')
    .onPostgresChanges(
        event: PostgresChangeEvent.insert,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Change received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to updates

supabase
    .channel('public:countries')
    .onPostgresChanges(
        event: PostgresChangeEvent.update,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Change received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to deletes

supabase
    .channel('public:countries')
    .onPostgresChanges(
        event: PostgresChangeEvent.delete,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Change received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to multiple events

supabase
    .channel('public:countries')
    .onPostgresChanges(
        event: PostgresChangeEvent.insert,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Insert event received: $\{payload.toString()\}');
        \})
    .onPostgresChanges(
        event: PostgresChangeEvent.delete,
        schema: 'public',
        table: 'countries',
        callback: (payload) \{
          print('Delete event received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to row level changes

supabase
    .channel('public:countries:id=eq.200')
    .onPostgresChanges(
        event: PostgresChangeEvent.delete,
        schema: 'public',
        table: 'countries',
        filter: PostgresChangeFilter(
          type: PostgresChangeFilterType.eq,
          column: 'id',
          value: 200,
        ),
        callback: (payload) \{
          print('Change received: $\{payload.toString()\}');
        \})
    .subscribe();

Listen to broadcast messages

supabase
    .channel('room1')
    .onBroadcast(
        event: 'cursor-pos',
        callback: (payload) \{
          print('Cursor position received!: $payload');
        \})
    .subscribe();

Listen to presence events

final channel = supabase.channel('room1');

channel.onPresenceSync((payload) \{
  print('Synced presence state: $\{channel.presenceState()\}');
\}).onPresenceJoin((payload) \{
  print('Newly joined presences $payload');
\}).onPresenceLeave((payload) \{
  print('Newly left presences: $payload');
\}).subscribe((status, error) async \{
  if (status == RealtimeSubscribeStatus.subscribed) \{
    await channel.track(\{'online_at': DateTime.now().toIso8601String()\});
  \}
\});