Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aldis-ameriks/pg-notify
Postgres PubSub client using NOTIFY/LISTEN
https://github.com/aldis-ameriks/pg-notify
listen notify pg postgres postgresql pubsub
Last synced: about 2 months ago
JSON representation
Postgres PubSub client using NOTIFY/LISTEN
- Host: GitHub
- URL: https://github.com/aldis-ameriks/pg-notify
- Owner: aldis-ameriks
- License: mit
- Created: 2020-10-24T19:06:07.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-06-13T19:04:53.000Z (7 months ago)
- Last Synced: 2024-11-29T19:07:14.282Z (about 2 months ago)
- Topics: listen, notify, pg, postgres, postgresql, pubsub
- Language: JavaScript
- Homepage:
- Size: 125 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
pg-notify
> Postgres PubSub client using NOTIFY/LISTEN
## Features
- Auto reconnect
- Payload size validation
- Channel and payload sanitization## Install
```sh
npm install pg-notify
```
```sh
yarn add pg-notify
``````sh
pnpm add pg-notify
```## Usage
> PGPubSub accepts the same config as [pg](https://github.com/brianc/node-postgres).
```js
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()
})()
```## API
### new PubSub(options)
- `options` (`object`) Configuration options for pg-notify pubsub instance. Accepts same options as [pg](https://github.com/brianc/node-postgres) with few custom ones described below.
- reconnectMaxRetries (`number`) Maximum number of reconnect attempts after losing connection. Pass `0` 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.](https://www.postgresql.org/docs/current/sql-notify.html)).### emit(channel, payload)
- `channel` (`string`)
- `payload` (`string` or `object`)### on(channel, listener)
- `channel` (`string`)
- `listener` (`function` accepting single argument `payload`)### removeListener(listener)
- `listener` (`function` accepting single argument `payload`)### close()
### connect()## Contributing
Contributions, issues and feature requests are welcome!
## License
Copyright © 2020 [Aldis Ameriks](https://github.com/aldis-ameriks).
This project is [MIT](https://github.com/aldis-ameriks/pg-notify/blob/main/LICENSE) licensed.