pg-subscribe
TypeScript icon, indicating that this package has built-in type declarations

1.2.1 • Public • Published

pg-subscribe

CircleCI JavaScript Style Guide semantic-release Greenkeeper badge

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.

install

npm install pg-subscribe --save

usage

basic

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.

advanced

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!
})

api

a copy of the latest typings can always be found here

Package Sidebar

Install

npm i pg-subscribe

Weekly Downloads

0

Version

1.2.1

License

MIT

Unpacked Size

36.3 kB

Total Files

18

Last publish

Collaborators

  • cdaringe