supabase / realtime-js

An isomorphic Javascript client for Supabase Realtime server.
https://supabase.com
MIT License
322 stars 57 forks source link

Supabase Realtime not working with RLS #254

Open RentfireFounder opened 1 year ago

RentfireFounder commented 1 year ago

Bug report

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

kamilogorek commented 1 year ago

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');
RentfireFounder commented 1 year ago

@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 commented 1 year ago

@kamilogorek Any update on this?

filipecabaco commented 1 year ago

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

RentfireFounder commented 1 year ago

@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: https://github.com/supabase/realtime-js/issues/254#issuecomment-1773552145

w3b6x9 commented 1 year ago

@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 commented 1 year ago

@w3b6x9 Yap!

RentfireFounder commented 1 year ago

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

filipecabaco commented 11 months ago

@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 commented 10 months ago

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 commented 10 months ago

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 commented 10 months ago

@filipecabaco @w3b6x9 i am using localhost

filipecabaco commented 7 months ago

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 commented 7 months ago

@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 commented 7 months ago

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 🤔