Postgres PubSub client using NOTIFY/LISTEN
- Auto reconnect
- Payload size validation
- Channel and payload sanitization
npm install pg-notify
yarn add pg-notify
pnpm add pg-notify
PGPubSub accepts the same config as pg.
import PGPubSub from 'pg-notify'
//const PGPubSub = require('pg-notify')
;(async () => {
const pubsub = new PGPubSub({
connectionString: 'postgres://postgres:postgres@localhost:5432/db'
})
await pubsub.connect()
await pubsub.on('test', (payload) => {
console.log('payload: ', payload)
})
await pubsub.emit('test', 'this is the payload')
await pubsub.emit('test', { foo: 'bar' })
await pubsub.close()
})()
-
options
(object
) Configuration options for pg-notify pubsub instance. Accepts same options as pg with few custom ones described below.- reconnectMaxRetries (
number
) Maximum number of reconnect attempts after losing connection. Pass0
to disable reconnecting. Default:10
. - maxPayloadSize (
number
) Maximum payload size, exceeding given size will throw an error. Default:7999
(In the default configuration it must be shorter than 8000 bytes.).
- reconnectMaxRetries (
-
channel
(string
) -
payload
(string
orobject
)
-
channel
(string
) -
listener
(function
accepting single argumentpayload
)
-
listener
(function
accepting single argumentpayload
)
Contributions, issues and feature requests are welcome!
Copyright © 2020 Aldis Ameriks.
This project is MIT licensed.