brianc / node-pg-pool

A connection pool for node-postgres
MIT License
180 stars 64 forks source link

Notification event does not work #40

Closed Globik closed 7 years ago

Globik commented 7 years ago

No event works with perform pg_notify.

rightaway commented 7 years ago

pool.on('notification', callback) doesn't work for you?

Globik commented 7 years ago

I don't know. But I'll try. I'm using node-pg-pool version 1.6.0 and node-pg-subpub

rightaway commented 7 years ago

Try getting the client from the pool, it works for me. Here's an ES6 example.

  const client = await pool.connect()
  client.on('notification', msg => {
    console.log(msg)
  })
  client.query('LISTEN channel')
brianc commented 7 years ago

I would recommend strongly against using a pooled client for listen/notify. For the listener instantiate a stand-alone client and connect it. The pool will close clients which haven't been checked out after a configurable interval - including any clients which you have have called listen on.

I have it on my roadmap to document proper usage of listen/notify - for now just don't use the pool for that client:

const listener = new pg.Client()
listener.connect()
listener.query('LISTEN channel')
listener.on('notification', msg => {
  // do your thing here
})