pglite icon indicating copy to clipboard operation
pglite copied to clipboard

JS can only listen to lower-case event channels

Open jamesgpearce opened this issue 6 months ago • 3 comments

Consider a trigger function that broadcasts to a channel with uppercase letters in it:

eg:

...PERFORM pg_notify('TinyBase', 'hello')...

Now try and listen to it with the pglite sdk:

await pglite.listen('TinyBase', listener);

Aaaand confusingly nothing happens. Because according to...

https://github.com/electric-sql/pglite/blob/71707ff970508fa1f8db6ed1d170f31194bf89e6/packages/pglite/src/utils.ts#L241-L242

...PG is case insensitive. I'm not sure about that (since I think it depends on how things are quoted) but apparently certainly not for event channels notified like this!

Convert the pg_notify string to lower case and it works great. But I don't think the same problem exists in other SDKs - certainly not postgres.js at least.

jamesgpearce avatar May 10 '25 03:05 jamesgpearce

I guess the intention is that the name should also be quoted in the argument since it had been quoted in the pg_notify context. But as you can see from the example, it's not intuitive. And in any case, single quotes are used in one place, and won't work in the other. Yuk :)

jamesgpearce avatar May 10 '25 03:05 jamesgpearce

@jamesgpearce Thank you for this!

We'll have to see what the "right" solution is here. Quoting the channel name works, there's even CI tests that we run for that.

tdrz avatar May 14 '25 09:05 tdrz

I'm not clear why there's a need to lowercase the string in that function at all (if it's insensitive as claimed). Seems like other SDKs do not.

Also it seems like single quotes have to be used in pg_notify but double quotes would have to be used in listen. Either way could probably close this out with a comment in the docs.

jamesgpearce avatar May 14 '25 22:05 jamesgpearce