{"id":13516566,"url":"https://github.com/voxpelli/node-pg-pubsub","last_synced_at":"2025-05-14T20:10:57.093Z","repository":{"id":24257925,"uuid":"27651656","full_name":"voxpelli/node-pg-pubsub","owner":"voxpelli","description":"A Publish/Subscribe implementation on top of PostgreSQL NOTIFY/LISTEN","archived":false,"fork":false,"pushed_at":"2025-02-15T04:47:25.000Z","size":136,"stargazers_count":240,"open_issues_count":18,"forks_count":16,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-13T17:46:42.754Z","etag":null,"topics":["node-js","postgres","pubsub"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/pg-pubsub","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/voxpelli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["voxpelli"]}},"created_at":"2014-12-06T22:43:14.000Z","updated_at":"2025-03-19T01:49:47.000Z","dependencies_parsed_at":"2023-12-26T02:23:55.633Z","dependency_job_id":"4216f791-60f5-4278-ae16-7ac8d27f5160","html_url":"https://github.com/voxpelli/node-pg-pubsub","commit_stats":{"total_commits":197,"total_committers":7,"mean_commits":"28.142857142857142","dds":"0.24873096446700504","last_synced_commit":"1a8187477fcbe8cdc6198f83b1d48e49e722edc8"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voxpelli%2Fnode-pg-pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voxpelli%2Fnode-pg-pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voxpelli%2Fnode-pg-pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/voxpelli%2Fnode-pg-pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/voxpelli","download_url":"https://codeload.github.com/voxpelli/node-pg-pubsub/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036802,"owners_count":22003651,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node-js","postgres","pubsub"],"created_at":"2024-08-01T05:01:23.638Z","updated_at":"2025-05-14T20:10:57.056Z","avatar_url":"https://github.com/voxpelli.png","language":"JavaScript","funding_links":["https://github.com/sponsors/voxpelli"],"categories":["JavaScript"],"sub_categories":[],"readme":"# PG PubSub\n\nA Publish/Subscribe implementation on top of [PostgreSQL NOTIFY/LISTEN](https://www.postgresql.org/docs/current/sql-notify.html)\n\n[![npm version](https://img.shields.io/npm/v/pg-pubsub.svg?style=flat)](https://www.npmjs.com/package/pg-pubsub)\n[![npm downloads](https://img.shields.io/npm/dm/pg-pubsub.svg?style=flat)](https://www.npmjs.com/package/pg-pubsub)\n[![Module type: CJS](https://img.shields.io/badge/module%20type-cjs-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)\n[![Types in JS](https://img.shields.io/badge/types_in_js-yes-brightgreen)](https://github.com/voxpelli/types-in-js)\n[![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg)](https://github.com/voxpelli/eslint-config)\n[![Follow @voxpelli@mastodon.social](https://img.shields.io/mastodon/follow/109247025527949675?domain=https%3A%2F%2Fmastodon.social\u0026style=social)](https://mastodon.social/@voxpelli)\n\n## Installation\n\n```bash\nnpm install pg-pubsub --save\n```\n\n## Requirements\n\n* Postgres \u003e= 9.4\n\n## Usage\n\n```js\nconst PGPubsub = require('pg-pubsub');\nconst pubsubInstance = new PGPubsub(uri[, options]);\n```\n\n### Options\n\n```js\n{\n  [log]: Function // default: silent when NODE_ENV=production, otherwise defaults to console.log(...)\n}\n```\n\n### Methods\n\n* **addChannel(channelName[, eventListener])** – starts listening on a channel and optionally adds an event listener for that event. As `PGPubsub` inherits from `EventEmitter` one can also add it oneself. Returns a `Promise` that resolves when the listening has started.\n* **removeChannel(channelName[, eventListener])** – either removes all event listeners and stops listeneing on the channel or removes the specified event listener and stops listening on the channel if that was the last listener attached.\n* **publish(channelName, data)** – publishes the specified data JSON-encoded to the specified channel. It may be better to do this by sending the `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.\n* **close(): Promise\u003cvoid\u003e** – closes down the database connection and removes all listeners. Useful for graceful shutdowns.\n* All [EventEmitter methods](http://nodejs.org/api/events.html#events_class_events_eventemitter) are inherited from `EventEmitter`\n\n### Examples\n\n#### Simple\n\n```javascript\nconst pubsubInstance = new PGPubsub('postgres://username@localhost/database');\n\nawait pubsubInstance.addChannel('channelName', function (channelPayload) {\n  // Process the payload – if it was JSON that JSON has been parsed into an object for you\n});\n\nawait pubsubInstance.publish('channelName', { hello: \"world\" });\n```\n\nThe above sends `NOTIFY channelName, '{\"hello\":\"world\"}'` to PostgreSQL, which will trigger the above listener with the parsed JSON in `channelPayload`.\n\n#### Advanced\n\n```javascript\nconst pubsubInstance = new PGPubsub('postgres://username@localhost/database');\n\nawait pubsubInstance.addChannel('channelName');\n\n// pubsubInstance is a full EventEmitter object that sends events on channel names\npubsubInstance.once('channelName', channelPayload =\u003e {\n  // Process the payload\n});\n```\n\n## Description\n\nCreating 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.\n\n## Lint / Test\n\n- setup a postgres database to run the integration tests\n  - the easist way to do this is via docker, `docker run -it -p 5432:5432 -e POSTGRES_DB=pgpubsub_test postgres`\n- `npm test`\n\nFor an all-in-one command, try:\n```sh\n# fire up a new DB container, run tests against it, and clean it up!\ndocker rm -f pgpubsub_test || true \u0026\u0026 \\\ndocker run -itd -p 5432:5432 -e POSTGRES_DB=pgpubsub_test --name pgpubsub_test postgres \u0026\u0026 \\\nnpm test \u0026\u0026 \\\ndocker rm -f pgpubsub_test\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoxpelli%2Fnode-pg-pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvoxpelli%2Fnode-pg-pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvoxpelli%2Fnode-pg-pubsub/lists"}