Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

README

        

pg-notify




Version


Maintenance


License: MIT


Coverage Status


CI

> 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.