package
A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN
npm install pg-pubsubGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN
npm install pg-pubsub --saveconst PGPubsub = require('pg-pubsub');
const pubsubInstance = new PGPubsub(uri[, options]);{
[log]: Function // default: silent when NODE_ENV=production, otherwise defaults to console.log(...)
}PGPubsub inherits from EventEmitter one can also add it oneself. Returns a Promise that resolves when the listening has started.NOTIFY channelName, '{"hello":"world"}' query yourself using your ordinary Postgres pool, rather than relying on the single connection of this module. Returns a Promise that will become rejected or resolved depending on the success of the Postgres call.EventEmitterconst pubsubInstance = new PGPubsub('postgres://username@localhost/database');
await pubsubInstance.addChannel('channelName', function (channelPayload) {
// Process the payload – if it was JSON that JSON has been parsed into an object for you
});
await pubsubInstance.publish('channelName', { hello: "world" });The above sends NOTIFY channelName, '{"hello":"world"}' to PostgreSQL, which will trigger the above listener with the parsed JSON in channelPayload.
const pubsubInstance = new PGPubsub('postgres://username@localhost/database');
await pubsubInstance.addChannel('channelName');
// pubsubInstance is a full EventEmitter object that sends events on channel names
pubsubInstance.once('channelName', channelPayload => {
// Process the payload
});Creating a PGPubsub instance will not do much up front. It will prepare itself to start a Postgres connection once the first channel is added and then it will keep a connection open until its shut down, reconnecting it if it gets lost, so that it can constantly listen for new notifications.
docker run -it -p 5432:5432 -e POSTGRES_DB=pgpubsub_test postgresnpm testFor an all-in-one command, try:
# fire up a new DB container, run tests against it, and clean it up!
docker rm -f pgpubsub_test || true && \
docker run -itd -p 5432:5432 -e POSTGRES_DB=pgpubsub_test --name pgpubsub_test postgres && \
npm test && \
docker rm -f pgpubsub_testEstimated unpacked size across 18 packages.
506.3 kB
Includes dependencies.
4 direct · 1 outdated