Search code examples
supabasesupabase-databasesupabase-jssupabase-realtime

Supabase Realtime - difference between subscribe() and channel()


The realtime docs overview shows this method to subscribe to changes:

// Initialize the JS client
import { createClient } from '@supabase/supabase-js'
const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

// Create a function to handle inserts
const handleInserts = (payload) => {
  console.log('Change received!', payload)
}

// Listen to inserts
const { data: todos, error } = await supabase.from('todos').on('INSERT', handleInserts).subscribe()

However, in the usage guide for Postgres Changes, there is a completely different method:

supabase
    .channel('schema-db-changes')
    .on(
      'postgres_changes',
      {
        event: '*',
        schema: 'public',
      },
      (payload) => console.log(payload)
    )
    .subscribe()

And in fact, I can't seem to find the first method anywhere else. How are they different? When should each be used? Thanks.


Solution

  • Thanks for finding this. The first method is an old deprecated method, and does not exist in the current client libraries. You should use the second method. I will update the docs.