Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T02:00:46.000Z (8 months ago)
- Last Synced: 2024-10-07T08:12:13.399Z (4 months ago)
- Topics: events, listen, notify, postgres, postgresql, pubsub, typescript
- Language: TypeScript
- Homepage:
- Size: 235 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# pg-subscribe
[![CircleCI](https://circleci.com/gh/cdaringe/pg-subscribe.svg?style=svg)](https://circleci.com/gh/cdaringe/pg-subscribe) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/cdaringe/pg-subscribe.svg)](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)