andywer/pg-listen

Payload must (?) be valid JSON

steve-baldwin opened this issue · 6 comments

Hi Andy,

Thanks a lot for your module and the work you've invested in it.

Are there any plans to support notification payloads that are not valid JSON? I was hoping to use it for passing an ID (UUID), but it barfs with:

  pg-listen:notification Received PostgreSQL notification on "b2bc-async": 9a87bf09-9e57-452c-869f-010481781f5b +0ms
Fatal database connection error: SyntaxError: Error parsing PostgreSQL notification payload: Unexpected token a in JSON at position 1
    at JSON.parse (<anonymous>)
    at Client.onNotification (/Users/stbaldwin/dev/node/node_modules/pg-listen/dist/index.js:116:51)
    at Client.emit (events.js:189:13)
    at Connection.<anonymous> (/Users/stbaldwin/dev/node/node_modules/pg/lib/client.js:315:10)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (/Users/stbaldwin/dev/node/node_modules/pg/lib/connection.js:125:12)
    at Socket.emit (events.js:189:13)
    at addChunk (_stream_readable.js:284:12)
    at readableAddChunk (_stream_readable.js:265:11)
    at Socket.Readable.push (_stream_readable.js:220:10)
  pg-listen:connection Closing PostgreSQL notification listener. +1m

I can possibly modify the notifying code to create a JSON payload, but it seems like an 'unnecessary' overhead, and it should be up to the consumer to decide what to do with the payload.

Cheers,

Steve

I have a legacy application where I cannot change the payload format, so I think that makes already two valid use cases for non-JSON payloads.

As a workaround I implemented an option to use "raw" payloads some time ago, but I am unsure if it is implemented up to standards of the author: master...Ingramz:master

Hey guys!

How about a serializer and deserializer option? Default would be JSON.stringify and JSON.parse, but you could pass any arbitrary function. In your use case maybe just the String constructor.

Check out #21. What do you think?

Hi Andy. Thanks for responding so quickly. I think that sounds like an awesome idea.

Yeah, looks good to me. The idea of serializer/deserializer suits well the packeted nature of the communication, allowing to return more complex objects as well.

Published as v1.3.0 🚀