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

postgres_changes event type does not scope data sent to client #589

Open
2 tasks done
acupofjose opened this issue Apr 28, 2023 · 1 comment
Open
2 tasks done

postgres_changes event type does not scope data sent to client #589

acupofjose opened this issue Apr 28, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@acupofjose
Copy link

acupofjose commented Apr 28, 2023

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

Documentation leads me to believe that setting a listener type ("UPDATE" or "INSERT") on postgres_changes would scope the data sent to the client to the event listener type.

However, when specifying a listen type it seems that ALL events on the table are sent via websocket to the client.
Note: The callback produces the correctly scoped result.

To Reproduce

Given the following table (with realtime enabled and RLS turned off):

create table
  public.test_realtime (
    id bigint generated by default as identity not null,
    data text null,
    created_at timestamp with time zone null default now(),
    constraint test_realtime_pkey primary key (id)
  ) tablespace pg_default;

And using the following code:

const url = "......."
const publicKey = "......"
const supabase = createClient(url, publicKey)

const channel = supabase.channel("testing_postgres")

channel.on("postgres_changes", { event: "UPDATE", schema: "public", table: "test_realtime" }, (data) => {
  console.log(`Called from UPDATE: ${JSON.stringify(data, null, 2)}`)
})

await channel.subscribe()

setTimeout(async () => {
  const table = supabase.from("test_realtime")
  const { data } = await table.insert({ data: `New Data inserted at ${new Date().toLocaleTimeString()}` }).select()

  if (data) {
    const row = data[0]
    await table.update({ data: `New Data updated at: ${new Date().toLocaleTimeString()}` }).eq("id", row.id)
    await table.delete().eq("id", row.id)
  }
}, 3000)

Produces the following websocket logs:

{"topic":"realtime:testing_postgres","event":"phx_join","payload":{"config":{"broadcast":{"ack":false,"self":false},"presence":{"key":""},"postgres_changes":[{"event":"UPDATE","schema":"public","table":"test_realtime"}]}},"ref":"1","join_ref":"1"}
{"event":"phx_reply","payload":{"response":{"postgres_changes":[{"id":46046838,"event":"UPDATE","schema":"public","table":"test_realtime"}]},"status":"ok"},"ref":"1","topic":"realtime:testing_postgres"}
{"event":"presence_state","payload":{},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"system","payload":{"channel":"testing_postgres","extension":"postgres_changes","message":"Subscribed to PostgreSQL","status":"ok"},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:36.979Z","errors":null,"record":{"created_at":"2023-04-28T02:20:36.978014+00:00","data":"New Data inserted at 9:20:36 PM","id":62},"schema":"public","table":"test_realtime","type":"INSERT"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:37.175Z","errors":null,"old_record":{"id":62},"record":{"created_at":"2023-04-28T02:20:36.978014+00:00","data":"New Data updated at: 9:20:37 PM","id":62},"schema":"public","table":"test_realtime","type":"UPDATE"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}
{"event":"postgres_changes","payload":{"data":{"columns":[{"name":"id","type":"int8"},{"name":"data","type":"text"},{"name":"created_at","type":"timestamptz"}],"commit_timestamp":"2023-04-28T02:20:37.301Z","errors":null,"old_record":{"id":62},"schema":"public","table":"test_realtime","type":"DELETE"},"ids":[46046838]},"ref":null,"topic":"realtime:testing_postgres"}

And the following console logs:

Called from UPDATE: {
  "schema": "public",
  "table": "test_realtime",
  "commit_timestamp": "2023-04-28T02:20:37.175Z",
  "eventType": "UPDATE",
  "new": {
    "created_at": "2023-04-28T02:20:36.978014+00:00",
    "data": "New Data updated at: 9:20:37 PM",
    "id": 62
  },
  "old": {
    "id": 62
  },
  "errors": null
}

Expected behavior

Websocket logs should not show events of type DELETE or INSERT when subscribed only to UPDATEs

System information

  • OS: Windows/Mac
  • Browser (if applies): Chrome
  • Version of supabase-js: 2.21.0
  • Version of Node.js: v18.14.0

Additional context

Also in reference to supabase-community/realtime-csharp#27

@acupofjose acupofjose added the bug Something isn't working label Apr 28, 2023
@acupofjose acupofjose changed the title postgres_changes listen type not respected in Websocket Responses postgres_changes event type does not scope data sent to client Apr 28, 2023
@filipecabaco
Copy link
Contributor

I wasn't able to replicate the issue, can you check if this behaviour has changed?

Screenshot 2023-11-17 at 19 13 57

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

2 participants