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

1.0.3 • Public • Published

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

npm install pg-notify
yarn add pg-notify
pnpm add pg-notify

Usage

PGPubSub accepts the same config as pg.

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 with few custom ones described below.

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.
This project is MIT licensed.

Package Sidebar

Install

npm i pg-notify

Weekly Downloads

216

Version

1.0.3

License

MIT

Unpacked Size

15.5 kB

Total Files

9

Last publish

Collaborators

  • aldis-ameriks