supabase/realtime

PostgresChanges subscription sends wrong tables to subscription after coming back from bring backgrounded

Opened this issue · 1 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

I'm using Supabase realtime for postgres_changes in a webview application on mobile. When I background the app, the client does not receive any events. When I re-enter the app before the socket has timed out, I get all of the events that were sent in the interim all at once.

The problem is the new data for a table is sent to every table's callback, not just the callback for that table. This only happens in the scenario when the app is recovering from being backgrounded and receiving events all at once.

To Reproduce

  1. Try to create a supabase client in the browser, subscribed to multiple tables
  2. Go to the app on a mobile device. Make sure to log the events from each table callback
  3. Background the app (go to home or a different app)
  4. Make some changes to the DB tables
  5. When you re-enter the app, check the logs. The changes you made to the DB tables should be published not only to the callbacks for those tables, but also all of the other callbacks
 this.dataChannel = this.supabase
      .channel("schema-db-changes")
      .on(
        "postgres_changes",
        {
          schema: "public",
          table: "table1",
          event: "*",
        },
        (e) => console.log("Table 1: ", e)
      )
      .on(
        "postgres_changes",
        {
          schema: "public",
          table: "table2",
          event: "*",
        },
        (e) => console.log("Table 2: ", e)
      )
      .on(
        "postgres_changes",
        {
          schema: "public",
          table: "table3",
          event: "*",
        },
        (e) => console.log("Table 3: ", e)
      )

Expected behavior

I would expect the data for table1 to only go to the callback for table1

Screenshots

These are screenshots of logs from my own application.

Screenshot 2024-11-04 at 9 47 48 PM

System information

  • OS: macOS
  • Browser: Chrome
  • Version of supabase-js: 2.46.1
  • Version of Node.js: 22.10.0

Additional context

Add any other context about the problem here.

I just had something similar. I had to put a guard up like this to actually check the table in the payload against the table I had subscribed to:

const channel = client
   .channel(tableName)
  .on(
    'postgres_changes',
    { event: 'UPDATE', schema: '*' },
    (payload) => {
      if (payload.table !== tableName) {
        // only handle if this is the table that we subscribed to
        return;
      }

      // handle the updates
    },
  )

I haven't had time to drill down into the exact scenario that causes it. It only happens in one place in my app.

I'm on desktop
Windows.
Supabase version 1.200.3