Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supabase Realtime not working with RLS #254

Open
2 tasks done
RentfireFounder opened this issue Oct 10, 2023 · 18 comments
Open
2 tasks done

Supabase Realtime not working with RLS #254

RentfireFounder opened this issue Oct 10, 2023 · 18 comments
Labels
bug Something isn't working

Comments

@RentfireFounder
Copy link

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Supabase Realtime doesn't get emit events when provided with table name, but works without table name in localhost

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Went to /database/replication and turned on supabase_realtime
Screenshot 2023-10-10 at 3 07 12 PM
  1. Turned on table for which I want realtime events
Screenshot 2023-10-10 at 3 07 20 PM

In Javascript, If I do something like this, this won't work

supabase
            .client 
            .channel('user_list_changes')
            .on(
                'postgres_changes',
                {
                    event: '*',
                    schema: 'public',
                    table: 'users_list',
                },
                (payload: any) => console.log('payload', payload),
            )
            .subscribe();

but this would

supabase
           .client 
           .channel('user_list_changes')
           .on(
               'postgres_changes',
               {
                   event: '*',
                   schema: 'public',
               },
               (payload: any) => console.log('payload', payload),
           )
           .subscribe();

Expected behavior

I was expecting to receive events when provided with table_name

Screenshots

If applicable, add screenshots to help explain your problem.

System information

  • OS: Mac
  • Browser: Brave
  • Version of supabase-js:
   "@supabase/supabase-js@^2.10.0":
   "@supabase/functions-js" "^2.1.0"
    "@supabase/gotrue-js" "^2.46.1"
    "@supabase/postgrest-js" "^1.8.0"
    "@supabase/realtime-js" "^2.7.4"
    "@supabase/storage-js" "^2.5.1"
    ```
- Version of Node.js: 18.3.0

@RentfireFounder RentfireFounder added the bug Something isn't working label Oct 10, 2023
@saltcod saltcod transferred this issue from supabase/supabase Oct 10, 2023
@kamilogorek
Copy link
Member

kamilogorek commented Oct 11, 2023

I cannot reproduce this issue. Are you able to share a concrete repro-case and possibly console logs?

image

supabase.channel('filtered-channel')
  .on(
    'postgres_changes',
    {
      event: '*',
      schema: 'public',
      table: 'users_list'
    },
    (payload) => console.log('Filtered Channel Payload:', payload),
  )
  .subscribe();

supabase.channel('global-channel')
  .on(
    'postgres_changes',
    {
      event: '*',
      schema: 'public',
    },
    (payload) => console.log('Global Channel Payload:', payload),
  )
  .subscribe();

console.log('Listening for events');

@kangmingtay kangmingtay transferred this issue from supabase/supabase-js Oct 13, 2023
@RentfireFounder
Copy link
Author

RentfireFounder commented Oct 21, 2023

@kamilogorek

Both of these don't log on update when I have enabled RLS policy on table

    React.useEffect(() => {
        console.log('here');
        serviceSubscription.current = supabase
            .getClient()
            .channel(`service-data-${serviceName}`)
            .on(
                'postgres_changes',
                {
                    event: '*',
                    schema: 'public',
                    table: 'services_connected',
                },
                (payload) => console.log('Change', payload),
            )
            .subscribe();

        supabase
            .getClient()
            .channel(`service-data`)
            .on(
                'postgres_changes',
                {
                    event: '*',
                    schema: 'public',
                },
                (payload) => console.log('Global', payload),
            )
            .subscribe();
    }, []);

Where

    
class Supabase {
  client: SupabaseClient<Database>;

  constructor() {
    const token = localStorage.getItem(SUPABASE_LOCAL_STORAGE_TOKEN_NAME);
    if (token) {
      this.client = createClient<Database>(VITE_SUPABASE_URL, VITE_SUPABASE_ANON_KEY, {
        global: {
          headers: {
            Authorization: `Bearer ${token}`,
          },
        },
      });
    } else {
      this.client = createClient<Database>(VITE_SUPABASE_URL, VITE_SUPABASE_ANON_KEY);
    }
  }

  createClient(authToken: string) {
    this.client = createClient<Database>(VITE_SUPABASE_URL, VITE_SUPABASE_ANON_KEY, {
      global: {
        headers: {
          Authorization: `Bearer ${authToken}`,
        },
      },
    });
  }

  getClient(): SupabaseClient<Database> {
    return this.client;
  }
}

const supabase = new Supabase();

Here is my RLS policy. (Normal Select queries work as expected).

Screenshot 2023-10-21 at 5 52 35 AM Screenshot 2023-10-21 at 5 52 21 AM

@RentfireFounder RentfireFounder changed the title Supabase Realtime not working with table name Supabase Realtime not working with RLS Oct 21, 2023
@RentfireFounder
Copy link
Author

@kamilogorek Any update on this?

@filipecabaco
Copy link
Member

@RentfireFounder it's working locally but not working when using Supabase infrastructure?

@RentfireFounder
Copy link
Author

@filipecabaco no it's working when RLS is disabled but not working when RLS is enabled.

With RLS enabled, the select/update and normal queries work but realtime doesn't

if this is the problem with my RLS rules, why is normal queries working and not realtime?

Also, more on this comment: #254 (comment)

@w3b6x9
Copy link
Member

w3b6x9 commented Oct 30, 2023

@RentfireFounder just to confirm you're spinning up the Supabase stack locally correct? This is a locally running version of Realtime that RLS is not working for you. Is that right?

@RentfireFounder
Copy link
Author

@w3b6x9 Yap!

@RentfireFounder
Copy link
Author

@w3b6x9 @filipecabaco @kamilogorek were you able to replicate it? any updatea?

@filipecabaco
Copy link
Member

@RentfireFounder sorry for the radio silence, we had a couple of outstanding issues...

One of them could be related with this: what is the URL you are using to connect?

@victorbillaud
Copy link

victorbillaud commented Dec 12, 2023

Hello, i got the same error... my RLS are well executed (log in a postgres function) but the realtime does not return the change @filipecabaco

@roxxid
Copy link

roxxid commented Dec 29, 2023

Exact same thing is happening with me, (working with only prod) with RLS turned on i only get DELETE events. No matter what RLS policy I add I dont see any events coming in. Like the OP said on turning RLS off again postgres changes start flowing again.

UPDATE: I added a SELECT policy with anon role set as TRUE (not recommended but it worked for now and can carry on with the dev)

@RentfireFounder
Copy link
Author

RentfireFounder commented Jan 3, 2024

@filipecabaco @w3b6x9 i am using localhost

@filipecabaco
Copy link
Member

hey everyone, sorry we were working on a lot of changes and were not able to dedicate enough time to close this issue.

is this issue persisted? were you able to tackle it?

@RentfireFounder
Copy link
Author

@filipecabaco nope, Also, just in case, did you see this comment? could it be because I am creating my own jwt and using that>

@filipecabaco
Copy link
Member

We do have an issue currently with custom jwts where the errors are not properly shown to the user 😞 I wonder if this is related 🤔

@rogaha
Copy link

rogaha commented Jan 17, 2025

Exact same thing is happening with me, (working with only prod) with RLS turned on i only get DELETE events. No matter what RLS policy I add I dont see any events coming in. Like the OP said on turning RLS off again postgres changes start flowing again.

UPDATE: I added a SELECT policy with anon role set as TRUE (not recommended but it worked for now and can carry on with the dev)

I'm having the same issue. Any updates here?

@kafkas
Copy link

kafkas commented Jan 28, 2025

Having the same issue. Has anyone been able to solve this?

@therealtimhawkins
Copy link

Same for me, anyone had any luck?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants