supabase: Realtime Subscriptions not working locally

Bug report

Describe the bug

“SUBSCRIBED” is returned to the client side, but the event cannot be listen. and I don’t know why, but the realtime.subscription table does not exist in the local DB.

It is working fine in production.

To Reproduce

  1. Checked “Enable Realtime” for the table in local Supabase Studio
  2. Write the following code
'use client';

import { createBrowserSupabaseClient } from '@supabase/auth-helpers-nextjs';

export default function Layout({ children }: { children: React.ReactNode }) {
    const supabase = createBrowserSupabaseClient({
        supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
        supabaseKey: process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
    });

    useEffect(() => {
        const channel = supabase
            .channel('companies')
            .on(
                'postgres_changes',
                {
                    event: 'UPDATE',
                    schema: 'public',
                    table: 'companies',
                },
                (_) => {
                    console.log('do something');
                },
            )
            .subscribe((status) => {
                console.log('Channel status', status);
            });
        console.log('Channel info', channel);

        return () => {
            supabase.removeAllChannels();
        };
    }, []);

    return { children };
}
  1. yarn build && yarn start

Expected behavior

Able to listen for changes in the database.

Screenshots

CleanShot 2023-02-20 at 13 02 26

System information

  • OS: macOS 13.2
  • Browser: chrome 109.0.5414.119
  • Version of supabase-js: 2.8.0
  • Version of Node.js: 16.14.2
  • Version of auth-helpers-nextjs: 0.5.4
  • Version of Next.js: 13.1.7-canary.7
  • Version of Supabase CLI: 1.38.4

Additional context

Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 5
  • Comments: 21 (2 by maintainers)

Most upvoted comments

Here are the general troubleshooting steps I use when trying to debug Realtime issues, which have prooven pretty helpful so far (some may sound mundane, but I tend to make dumb mistakes myself and this list usually helps me catch them)

  1. is Realtime enabled on the specific table (I cannot tell you how many times I forget to check the box turning it on)
  2. did I remember to call the .subscribe() method at the end of the function creating the subscription (this is for JavaScript, I am not familiar with other frameworks for Supabase)
  3. The .subscribe() method has a callback for status updates, perhaps the subscription is failing for a network/firewall issue; i.e., something like:
.subscribe((status, err) => { 
  if(err) console.err("SUBSCRIPTION ERROR:". err);
  else console.log("SUBSCRIPTION STATUS CHANGED:", status);
})
  1. is the ID I chose for the subscription (in javascript, the ID use in supabase.channel(SUBSCRIPTION_ID). ... showing up under the public.realtime table?
  2. are the filters applied locally the same/correctly showing in the public.realtime table?
  3. have I misspelled or improperly formatted the filter for the subscription (not much type-checking in JavaScript/TypeScript since the filter is a raw string, so the simplest formatting error or column-naming error can cause the subscription to behave in unexpected ways)
  4. is the realtime container running (stop and restart supabase, use docker’s CLI or GUI to make sure all supabase containers are running, and none have failed)
  5. Realtime does respect any RLS, so are my RLS rules otherwise working (does SELECT still work, but just the Realtime fails?)
  6. Is there a new CLI version I can update to (for Supabase)? If so, make sure to stop the old docker instance, do the update, (perhaps even reboot your computer for safe-practices), restart Docker, and then (and only then) start-up the new CLI update (supabase start)

Hope this helps; this list is not exhaustive, but 90% of the time is enough to catch the errors I’m experiencing.

I switched to Supabase triggers and my problem is gone. Webhooks solve the day

I could fix it for me, I updated the supabase cli, deleted all container did a fresh supabase init and then it worked.

I am on ^1.68.6 and was still experiencing the issue

In fact, I have tried almost every version in the last 4 months hoping the my issue would go away but it didn’t.

With the exact same environment setup steps, realtime just sometimes worked and sometimes didn’t in, seemingly random fashion.

Same here, works on hosted but not in local dev