supabase / realtime-js

An isomorphic Javascript client for Supabase Realtime server.

Home Page:https://supabase.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Supabase Realtime not working with RLS

RentfireFounder opened this issue · comments

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

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');

@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

@kamilogorek Any update on this?

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

@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)

@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?

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

@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?

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

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)

@filipecabaco @w3b6x9 i am using localhost

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?

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

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 🤔