{"id":26959132,"url":"https://github.com/cbschuld/valkey-pubsub","last_synced_at":"2025-10-03T20:33:16.471Z","repository":{"id":283887556,"uuid":"953201510","full_name":"cbschuld/valkey-pubsub","owner":"cbschuld","description":"A lightweight, TypeScript-native Pub/Sub implementation designed for use with Mercurius GraphQL, built on top of Valkey (Redis-compatible).","archived":false,"fork":false,"pushed_at":"2025-03-22T20:27:04.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T20:29:27.530Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/cbschuld.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}},"created_at":"2025-03-22T19:56:10.000Z","updated_at":"2025-03-22T20:26:21.000Z","dependencies_parsed_at":"2025-03-22T20:41:23.958Z","dependency_job_id":null,"html_url":"https://github.com/cbschuld/valkey-pubsub","commit_stats":null,"previous_names":["cbschuld/valkey-pubsub"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbschuld%2Fvalkey-pubsub","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbschuld%2Fvalkey-pubsub/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbschuld%2Fvalkey-pubsub/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cbschuld%2Fvalkey-pubsub/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cbschuld","download_url":"https://codeload.github.com/cbschuld/valkey-pubsub/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246939225,"owners_count":20857916,"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":[],"created_at":"2025-04-03T04:34:48.156Z","updated_at":"2025-10-03T20:33:11.412Z","avatar_url":"https://github.com/cbschuld.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# valkey-pubsub\n\nA lightweight, TypeScript-native Pub/Sub implementation designed for use with [Mercurius GraphQL](https://github.com/mercurius-js/mercurius), built on top of Valkey (Redis-compatible).\n\nSupports broadcasting messages to multiple listeners and enables easy event-driven design for GraphQL subscriptions or internal messaging systems.\n\n---\n\n## Features\n\n- 🔁 **Broadcast-style delivery**: All listeners receive each message\n- ⚡ **Fast and simple API**: Push, subscribe, destroy\n- ✅ **Compatible with Mercurius** PubSub interface\n- 🧪 **Tested with Jest**\n- 🧱 **Type-safe and modular** — written in pure TypeScript\n\n---\n\n## Installation\n\n```bash\npnpm add valkey-pubsub\n```\n\n## Usage\n\n### Create the PubSub instance\n\n```typescript\nimport ValkeyPubSub, { PubSubGenericQueue } from \"valkey-pubsub\";\n\nconst pubsub = await ValkeyPubSub.create({\n  addresses: [{ host: \"localhost\", port: 6379 }],\n  clusterMode: false,\n});\n```\n\n### Subscribe to a topic\n\n```typescript\nconst queue = new PubSubGenericQueue\u003cstring\u003e();\n\nawait pubsub.subscribe(\"my-topic\", queue);\n\nqueue.onItem((message) =\u003e {\n  console.log(\"Received:\", message);\n});\n```\n\n### Publish a message\n\n```typescript\nawait pubsub.publish({\n  topic: \"my-topic\",\n  payload: { hello: \"world\" },\n});\n```\n\n## Usage with Mercurius\n\nThe original/driving force for the design was the [subscription models for Mercurius](https://mercurius.dev/#/docs/subscriptions)\n\n```typescript\nconst pubsub = await ValkeyPubSub.create();\n\nfastify.decorate(\"pubsub\", pubsub);\n\nfastify.register(mercurius, {\n  schema,\n  resolvers: await resolvers,\n  loaders: await loaders,\n  context: async (request: FastifyRequest) =\u003e {\n    return {\n      request,\n      db: server.db,\n      valkey: server.valkey,\n      pubsub: server.pubsub,\n      logger: server.log,\n    } as ServerDecorators;\n  },\n  subscription: {\n    context: async (_server, request) =\u003e {\n      return {\n        request,\n        db: server.db,\n        valkey: server.valkey,\n        pubsub: server.pubsub,\n        logger: server.log,\n      } as ServerDecorators;\n    },\n    pubsub: server.pubsub,\n  },\n  graphiql: false, // ℹ️ cannot use in place with helmet\n  allowBatchedQueries: true,\n  path: \"/graphql\", // 👈 Restricts GraphQL to this endpoint\n  prefix: \"/\",\n});\n```\n\n## API\n\n### ValkeyPubSub\n\n#### `ValkeyPubSub.create(config): Promise\u003cValkeyPubSub\u003e`\n\nCreates a PubSub instance.\n\nConfig options:\n\n- addresses: Array of { host, port } objects (defaults to Valkey on localhost:6379)\n- protocol: 'RESP2' or 'RESP3' (default: 'RESP3')\n- clusterMode: true or false (default: false)\n\n#### `pubsub.subscribe(topic, queue)`\n\n- Subscribes a queue to a topic. Every published message will be pushed to the queue and delivered to all its listeners.\n\n#### `pubsub.publish({ topic, payload }, callback?)`\n\n- Publishes a message to the specified topic. Payload will be stringified before being sent.\n\n#### `pubsub.cleanup()`\n\n- Cleans up all open connections and subscriptions.\n\n### PubSubGenericQueue\n\n#### `PubSubGenericQueue\u003cT\u003e`\n\nA generic in-memory delivery queue that stores items and allows multiple listeners.\n\nMethods:\n\n- `push(value: T)`: Pushes a value onto the queue\n- `onItem(callback: (value: T) =\u003e void)`: Registers a callback for new items\n- `isEmpty()`: Returns true if the queue is empty\n- `size()`: Returns the current number of pending items\n- `destroy()`: Destroys the queue and runs any registered close callbacks\n\n## License\n\nMIT © Chris Schuld\n\n## Contact\n\n- **Email** - twitter handle @ gmail.com\n- **X** - @cbschuld\n\n## Contributing\n\nYes, thank you! Please update the docs and tests and add your name to the package.json file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbschuld%2Fvalkey-pubsub","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcbschuld%2Fvalkey-pubsub","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcbschuld%2Fvalkey-pubsub/lists"}