npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pgee

v1.0.1

Published

PostgreSQL Asynchronous Notification Event Emitter

Downloads

13

Readme

pgee

Current Version Build Status via Travis CI Dependencies

belly-button-style

PostgreSQL asynchronous notification event emitter. pgee allows PostgreSQL's LISTEN and NOTIFY features to be used as a Node.js EventEmitter.

An example use of pgee is shown below. pgee exports a single constructor that extends EventEmitter. The constructor accepts anything that can be passed to pg.connect(), or an already connected pg.Client instance. The pgee.prototype.listen() and pgee.prototype.unlisten() methods are used to LISTEN and UNLISTEN channels in the database. Notifications received from the database are emitted as events. Similarly, the pgee.prototype.emit() method triggers a NOTIFY on the database channel.

const CONNECT_STRING = `postgres://${process.env.USER}@localhost/postgres`;
const PgEe = require('pgee');

// The constructor accepts a pg.Client, connection string, or any valid
// pg connection configuration.
const pgee = new PgEe(CONNECT_STRING);

pgee.connect((err) => {
  if (err) {
    return console.error(err);
  }

  // Listen on "foo" channel. When a notification is received, emit a "foo"
  // event, triggering the listener function.
  pgee.listen({
    channel: 'foo',
    listener: (data) => {
      console.log(`Received 'foo' event: ${JSON.stringify(data)}`);
    }
  }, (err, channel) => {
    if (err) {
      return console.error(err);
    }

    pgee.emit('foo', {bar: 'baz'});
  });
});

API

pgee(options) Constructor

  • Arguments
    • options (varies) - Options to pass to pg.connect() or a connected instance of pg.Client (see the pg module for details).
  • Returns
    • object - A newly constructed pgee instance.

Constructs a new pgee instance, which extends EventEmitter. The constructor does not attempt to connect to a database.

pgee.prototype.connect([callback])

  • Arguments
    • callback (function) - An optional callback function that is invoked after connecting to the database. The callback only receives a possible error argument.
  • Returns
    • Nothing

Connects to a PostgreSQL database using the credentials provided to the constructor. The connection is taken from the pg module's connection pool. If an instance of pg.Client was passed to the constructor, it is assumed to already be connected, so no further action is taken. If callback is a function, it is called upon completion. If a function was not provided, a 'connect' event is emitted.

pgee.prototype.close()

  • Arguments
    • None
  • Returns
    • Nothing

Closes the pgee instance. If the connection was taken from the pg connection pool during connect(), it will be returned to the pool. If an external pg.Client was provided to the constructor, no action will be taken on the client. All attached event listeners are removed from the instance.

pgee.prototype.listen(channel [, callback])

  • Arguments
    • channel (string or object) - If this is a string, it represents the name of the channel to subscribe to. If is is an option, it supports the following properties.
      • channel (string) - The channel name to subscribe to.
      • listener (function) - An optional event listener that triggers on channel events. The data associated with the database notification is passed as the only argument.
    • callback (function) - An optional callback function. If an error occurs, it is passed as the first argument of the callback. On success, the second argument of the callback is the channel name as a string.
  • Returns
    • Nothing

Issues a LISTEN "channel" to the database. If a listener is provided, it is attached using the on() method. Additional listeners can be attached later using on(), addListener(), once(), or listen(). After the LISTEN command is issued, the callback function is invoked (if one is present). If no callback was provided, a 'listen' event is emitted, containing the name of the channel that was added.

pgee.prototype.unlisten(channel [, callback])

  • Arguments
    • channel (string or object) - If this is a string, it represents the name of the channel to unsubscribe from. If is is an option, it supports the following properties.
      • channel (string) - The channel name to subscribe to.
      • removeListeners (boolean) - An optional value that, if true, removes all channel listeners. Defaults to false.
    • callback (function) - An optional callback function. If an error occurs, it is passed as the first argument of the callback. On success, the second argument of the callback is the channel name as a string.
  • Returns
    • Nothing

Issues an UNLISTEN "channel" to the database. If removeListeners is true, any existing channel listeners are removed using removeAllListeners(channel). Listeners can also be removed later using removeAllListeners(), unlisten(), etc. After the UNLISTEN command is issued, the callback function is invoked (if one is present). If no callback was provided, an 'unlisten' event is emitted, containing the name of the channel that was removed.

pgee.prototype.emit(event, message)

  • Arguments
    • event (string) - Name of the event to emit. If the event name is 'error', an error is emitted immediately. Any other value triggers a database notification, where the channel name is equal to event.
    • message (varies) - Data attached to the emitted event.
  • Returns
    • Nothing

Issues a pg_notify() to the database, where the channel name is event, and message represents the notification payload. If event is 'error', then no request is made to the database, and an error is immediately emitted. Note that the names 'connect', 'listen', and 'unlisten' have the potential to conflict with other events used in pgee.