https://github.com/cdaringe/pg-subscribe
subscribe to postgres events in node via LISTEN/NOTIFY apis
https://github.com/cdaringe/pg-subscribe
events listen notify postgres postgresql pubsub typescript
Last synced: 3 months ago
JSON representation
subscribe to postgres events in node via LISTEN/NOTIFY apis
- Host: GitHub
- URL: https://github.com/cdaringe/pg-subscribe
- Owner: cdaringe
- Created: 2018-09-11T23:35:06.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T02:00:46.000Z (about 1 year ago)
- Last Synced: 2025-03-26T23:08:52.398Z (3 months ago)
- Topics: events, listen, notify, postgres, postgresql, pubsub, typescript
- Language: TypeScript
- Homepage:
- Size: 235 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# pg-subscribe
[](https://circleci.com/gh/cdaringe/pg-subscribe) [](https://standardjs.com) [](https://github.com/semantic-release/semantic-release) [](https://greenkeeper.io/)
subscribe to postgres events via [postgresql notify/listen](http://www.postgresql.org/docs/10/static/sql-notify.html)
this is a fork of [pg-pub-sub](https://github.com/voxpelli/node-pg-pubsub) to address some open issues, provide 1st class typescript support, and improve the api.
## install
```sh
npm install pg-subscribe --save
```## usage
### basic
```js
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
```js
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](https://github.com/cdaringe/pg-subscribe/blob/master/docs/index.d.ts)