porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.39k stars 267 forks source link

Duplicate notifications when subscribing to a publication with postgresjs #752

Open ali-shabani opened 10 months ago

ali-shabani commented 10 months ago

I have created a publication in PostgreSQL and I'm using subscribe to listen to changes. I'm using this code:

const { unsubscribe } = await sql.subscribe(
  'update:users',
  (row, { command, relation, key, old }) => {
    console.log(row, key, old)
  }
)

The problem is that my callback is called twice when I'm updating a record on the users table. Also, I get false for key and null for old in both callbacks. Is there any bug in the implementation or did I do something wrong?

Expected behavior:

I expect the callback to be called only once when I update a record on the users table. I also expect to get the correct values for key and old in the callback.

Actual behavior:

The callback is called twice when I update a record on the users table. I get false for key and null for old in both callbacks.

Environment:

• postgres version: ^3.4.3

• PostgreSQL version: docker image: postgres:15.1

• Node.js version: v18.18.2

ali-shabani commented 10 months ago

I have created the publication using below command:

CREATE PUBLICATION alltables FOR ALL TABLES;

and I also tried to initialize the postgres with and without providing the publications options. the result was unchanged.

Louis-Tian commented 8 months ago

I think you will need to post more of your code for anyone to help you. I have been using the subscribe and never experienced any issue of that sort. It most likely to do with you other setups rather than postgres.js.