subscribe to postgres events via postgresql notify/listen
this is a fork of pg-pub-sub to address some open issues, provide 1st class typescript support, and improve the api.
npm install pg-subscribe --save
import { PgSubscriber } from 'pg-subscribe'
var subscriber = new PgSubscriber('postgres://username@localhost/database')
await subscriber.addChannel('channelName', function onNotify (channelPayload) {
// Process the payload – if it was JSON that JSON has been parsed into an object for you
})
await subscriber.publish('channelName', { hello: "world" })
the above sends NOTIFY channelName, '{"hello":"world"}'
to PostgreSQL, which will trigger the above listener with the parsed JSON in channelPayload
.
import { PgSubscriber } from 'pg-subscribe'
var subscriber = new PgSubscriber('postgres://username@localhost/database')
await subscriber.addChannel('channelName')
// subscriber is a full EventEmitter object that sends events on channel names
subscriber.once('channelName', function (channelPayload) {
// do great work!
})
a copy of the latest typings can always be found here