{"id":21147826,"url":"https://github.com/siamahnaf/graphql-pg-subscriptions","last_synced_at":"2025-08-01T17:36:32.724Z","repository":{"id":244996210,"uuid":"815636777","full_name":"siamahnaf/graphql-pg-subscriptions","owner":"siamahnaf","description":"This package implements the PubSubEngine Interface from the graphql-subscriptions package and also the new AsyncIterator interface. It allows you to connect your subscriptions manger to a postgres based Pub Sub mechanism to support multiple subscription manager instances.","archived":false,"fork":false,"pushed_at":"2024-11-21T07:04:10.000Z","size":14,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-18T11:13:51.829Z","etag":null,"topics":["graphql","graphql-subscriptions","pg-subscription-billing","postgresql","subscription"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/graphql-pg-subscriptions","language":"TypeScript","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/siamahnaf.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,"zenodo":null}},"created_at":"2024-06-15T17:20:56.000Z","updated_at":"2025-04-28T23:10:59.000Z","dependencies_parsed_at":"2024-11-20T09:43:18.234Z","dependency_job_id":"9fea4c7e-fd67-4461-9bc8-e4e3ca8d9bad","html_url":"https://github.com/siamahnaf/graphql-pg-subscriptions","commit_stats":null,"previous_names":["siamahnaf/graphql-pg-subscriptions"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/siamahnaf/graphql-pg-subscriptions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamahnaf%2Fgraphql-pg-subscriptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamahnaf%2Fgraphql-pg-subscriptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamahnaf%2Fgraphql-pg-subscriptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamahnaf%2Fgraphql-pg-subscriptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/siamahnaf","download_url":"https://codeload.github.com/siamahnaf/graphql-pg-subscriptions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/siamahnaf%2Fgraphql-pg-subscriptions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268268072,"owners_count":24222990,"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","status":"online","status_checked_at":"2025-08-01T02:00:08.611Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["graphql","graphql-subscriptions","pg-subscription-billing","postgresql","subscription"],"created_at":"2024-11-20T09:18:46.332Z","updated_at":"2025-08-01T17:36:32.669Z","avatar_url":"https://github.com/siamahnaf.png","language":"TypeScript","funding_links":["https://www.buymeacoffee.com/siamahnaf"],"categories":[],"sub_categories":[],"readme":"# Graphql PG Subscription\r\n\r\nA graphql subscriptions implementation using postgres and apollo's graphql-subscriptions.\r\n\r\nThis package implements the PubSubEngine Interface from the graphql-subscriptions package and also the new AsyncIterator interface. It allows you to connect your subscriptions manger to a postgres based Pub Sub mechanism to support multiple subscription manager instances.\r\n\r\n\r\n\u003ca href=\"https://www.buymeacoffee.com/siamahnaf\" target=\"_blank\"\u003e\u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png\" alt=\"Buy Me A Coffee\" style=\"height: 60px !important;width: 217px !important;\" \u003e\u003c/a\u003e\r\n\r\n# Features\r\n- Properly Maintained\r\n- Small In Size\r\n- Larger Payload Problem Solved (I use pg table for the payload. So now larger payload can pass) - (PG Default Notify has size limit, that's why you can't provide larger payload.);\r\n- Fully TypeScript Support\r\n- Worked with any framework (ExpressJS, NestJS)\r\n\r\n## Installation\r\n\r\n```bash\r\nnpm i graphql-pg-subscriptions\r\n```\r\n\r\n## Usage\r\n\r\nFirst of all, follow the instructions in [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) to add subscriptions to your app.\r\n\r\nAfterwards replace `PubSub` with `PostgresPubSub`:\r\n\r\n```js\r\n// Before\r\nimport { PubSub } from \"graphql-subscriptions\";\r\n\r\nexport const pubsub = new PubSub();\r\n```\r\n\r\n```js\r\n//After\r\nimport { PostgresPubSub } from \"graphql-postgres-subscriptions\";\r\nimport { Client } from \"pg\";\r\n\r\nconst client = new Client({\r\n    user: 'dbuser',\r\n    host: 'database.server.com',\r\n    database: 'mydb',\r\n    password: 'secretpassword',\r\n    port: 3211,\r\n});\r\n\r\nclient.connect();\r\n\r\nconst pubsub = new PostgresPubSub({ client, maxListeners: 15 });\r\n\r\n//You can increase max event listeners if you need, default is 15\r\n```\r\n\r\n### commonMessageHandler\r\n\r\nThe second argument to `new PostgresPubSub()` is the `commonMessageHandler`. The common message handler gets called with the received message from PostgreSQL.\r\nYou can transform the message before it is passed to the individual filter/resolver methods of the subscribers.\r\nThis way it is for example possible to inject one instance of a [DataLoader](https://github.com/facebook/dataloader) which can be used in all filter/resolver methods.\r\n\r\n```javascript\r\nconst getDataLoader = () =\u003e new DataLoader(...)\r\nconst commonMessageHandler = ({attributes: {id}, data}) =\u003e ({id, dataLoader: getDataLoader()})\r\nconst pubsub = new PostgresPubSub({ client, commonMessageHandler });\r\n```\r\n\r\n```javascript\r\nexport const resolvers = {\r\n  Subscription: {\r\n    somethingChanged: {\r\n      resolve: ({ id, dataLoader }) =\u003e dataLoader.load(id)\r\n    }\r\n  }\r\n};\r\n```\r\n\r\n## Error handling\r\n\r\n`PostgresPubSub` instances emit a special event called `\"error\"`. This event's payload is an instance of Javascript's `Error`. You can get the error's text using `error.message`.\r\n\r\n```js\r\nconst ps = new PostgresPubSub({ client });\r\n\r\nps.subscribe(\"error\", err =\u003e {\r\n  console.log(err.message); // -\u003e \"payload string too long\"\r\n}).then(() =\u003e ps.publish(\"a\", \"a\".repeat(9000)));\r\n```\r\n\r\nFor example you can log all error messages (including stack traces and friends) using something like this:\r\n\r\n```js\r\nps.subscribe(\"error\", console.error);\r\n```\r\n\r\n## Stay in touch\r\n\r\n- Author - [Siam Ahnaf](https://www.siamahnaf.com/)\r\n- Website - [https://www.siamahnaf.com/](https://www.siamahnaf.com/)\r\n- Twitter - [https://twitter.com/siamahnaf198](https://twitter.com/siamahnaf198)\r\n- Github - [https://github.com/siamahnaf](https://github.com/siamahnaf)\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiamahnaf%2Fgraphql-pg-subscriptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsiamahnaf%2Fgraphql-pg-subscriptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsiamahnaf%2Fgraphql-pg-subscriptions/lists"}