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

pg-hub

v0.0.2

Published

PostgreSQL Event Hub for NodeJS

Downloads

5

Readme

pg-hub

License: ISC stability-beta

Lightweight event infrastructure built on PostgreSQL Notify/Listen.

install

npm i pg-hub

overview

The pg-hub package provides lightweight event routing infrastructure based on PostgreSQL notify and listen. In basic use cases, it enables database triggers to send events to a node client application. In more sophisticated systems, the RDBMS becomes a top-level broker, enabling database logic and multiple node clients to produce and consume events.

Each event includes three fields, topic, keys, and value. The topic is an alphanumeric string that corresponds directly to a PSQL notification channel. The keys field contains an ordered list of 64-bit signed integers, and the value field is a JSON object. The keys and value fields are encoded into the notification payload, while the topic field corresponds to a PostgreSQL notification channel.

example

import Pg from 'pg'
import PgHub from 'pg-hub'

const pool = new Pg.Pool(config);
const hub = new PgHub.Hub(pool, 5000);

const result1 = await hub.start();

const consumer = hub.consumer((event) => console.log('topic=' + event.topic + ' key=' + event.key + ' value=' + event.value));
consumer.subscribe('topic1');

hub.notify('topic1');
hub.notify('topic1', 100n, {a:1, b:'text', c:[0,1,4]});
hub.notify('topic1', [101n,102n,103n], {a:2, b:{d:1, e:'xyzzy'}, c:[0,1,4]});
hub.notify('topic1', null, [1,2,3]);
hub.notify('topic1', 42n, null);

await new Promise((resolve) => setTimeout(resolve, 1000));

const result2 = await hub.stop();

nodejs details

The pg-hub object model includes Hub and Consumer object classes. The Hub class wraps a PostgreSQL connection using node-postgres. Hub objects support event producers with an outbound notify method, and they allow creation of event consumers with a consumer method. The Consumer class represents an event consumer. Consumer objects wrap a callback hook, which receives events based on one or more topic subscriptions.

hub object

A hub is created as follows:

import PgHub from 'pg-hub';
const hub = new PgHub.Hub(pool, reconnectTime, logger, name);

The Hub constructor includes four parameters, pool, reconnectTime, logger, and name. The pool parameter is a Pg.Pool object, used to connect to the PostgreSQL server. The reconnectTime parameter is the time, in milliseconds, that the hub waits between connection (or reconnection) attempts. If inluded, the logger parameter contains an object that exposes four logging methods, debug(), info(), warn(), and error(). The methods may be set to null to disable the corresponding level of logging, and the logger parameter may be omitted (or set to null) to disable logging altogether. The name parameter includes an optional symbolic name, which is exposed through the object's name property but not otherwise used by the hub object itself.

Once created, a hub exposes four public methods: start(), stop(), notify(), and consumer(). The start method opens a connection to the database, drawing a persistent client from the pool parameter passed during construction. The stop method likewise closes down the connection and releases associated resources, allowing the caller to await final completion. The notify method sends an outbound event, and the the consumer method creates a Consumer object to receive inbound events.

hub.start();
const consumer = hub.consumer(event => {});
hub.notify(topic, key, value);
await hub.stop();

consumer object

const consumer = hub.consumer(event => {});
consumer.subsribe('topic0');
consumer.subscribe(['topic1, 'topic2']);
consumer.close();

supervisory notifications

A hub can produce six supervisory notifications, received by consumers as events with a null topic, an empty set of keys, and a value object as follows:

{action:'connect',final:false}
{action:'disconnect',final:false}
{action:'reconnect',final:false}
{action:'stop',final:true}
{action:'subscribe',topic:'topic_name',final:false}
{action:'close',final:true}

The action property describes the cause of notification, and the final property indicates whether the notification is final. In the case of the subscribe action, the additional topic field indicates the topic of the new subscription.

The connect action informs consumers that the hub has connected to the RDBMS and started normal operations (see start() above). The disconnect action informs consumers that the hub has lost its connection, and the reconnect action informs consumers that the connection has been restored. During the gap between a disconnect and reconnect, events can be lost and consumers should act accordingly. The stop action informs consumers that their associated hub has stopped operation (see stop() above). The subscribe action informs the consumer that it has subscribed to the associated topic (see subscribe() above), and the close notification informs the consumer that it has been closed (see close() above).

PostgreSQL Interface

Underlying Event Details

Events are encoded into text format, compatible with the PostgreSQL notify command. Implementation details are available here