{"id":13727350,"url":"https://github.com/davidyaha/graphql-redis-subscriptions","last_synced_at":"2025-04-09T00:29:04.711Z","repository":{"id":37445262,"uuid":"67158863","full_name":"davidyaha/graphql-redis-subscriptions","owner":"davidyaha","description":"A graphql subscriptions implementation using redis and apollo's graphql-subscriptions ","archived":false,"fork":false,"pushed_at":"2024-09-10T16:28:57.000Z","size":1280,"stargazers_count":1108,"open_issues_count":40,"forks_count":126,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-10-29T14:57:14.529Z","etag":null,"topics":["graphql","graphql-subscriptions","redis","redis-subscriptions"],"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/davidyaha.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2016-09-01T19:08:18.000Z","updated_at":"2024-10-28T15:57:48.000Z","dependencies_parsed_at":"2024-01-07T21:06:42.364Z","dependency_job_id":"3e668824-0c7d-41d3-a32f-dfcefff3db27","html_url":"https://github.com/davidyaha/graphql-redis-subscriptions","commit_stats":{"total_commits":332,"total_committers":40,"mean_commits":8.3,"dds":0.7740963855421686,"last_synced_commit":"1775b7c6553ccfc7b64441d5772b227e621a7fe7"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidyaha%2Fgraphql-redis-subscriptions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidyaha%2Fgraphql-redis-subscriptions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidyaha%2Fgraphql-redis-subscriptions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidyaha%2Fgraphql-redis-subscriptions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidyaha","download_url":"https://codeload.github.com/davidyaha/graphql-redis-subscriptions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247949053,"owners_count":21023267,"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":["graphql","graphql-subscriptions","redis","redis-subscriptions"],"created_at":"2024-08-03T01:03:51.218Z","updated_at":"2025-04-09T00:29:04.690Z","avatar_url":"https://github.com/davidyaha.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# graphql-redis-subscriptions\n\n[![Build Status](https://travis-ci.org/davidyaha/graphql-redis-subscriptions.svg?branch=master)](https://travis-ci.org/davidyaha/graphql-redis-subscriptions)\n\nThis package implements the PubSubEngine Interface from the [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package and also the new AsyncIterator interface. \nIt allows you to connect your subscriptions manager to a Redis Pub Sub mechanism to support \nmultiple subscription manager instances.\n\n## Installation\nAt first, install the `graphql-redis-subscriptions` package: \n```\nnpm install graphql-redis-subscriptions\n```\n\nAs the [graphql-subscriptions](https://github.com/apollographql/graphql-subscriptions) package is declared as a peer dependency, you might receive warning about an unmet peer dependency if it's not installed already by one of your other packages. In that case you also need to install it too:\n```\nnpm install graphql-subscriptions\n```\n   \n## Using as AsyncIterator\n\nDefine your GraphQL schema with a `Subscription` type:\n\n```graphql\nschema {\n  query: Query\n  mutation: Mutation\n  subscription: Subscription\n}\n\ntype Subscription {\n    somethingChanged: Result\n}\n\ntype Result {\n    id: String\n}\n```\n\nNow, let's create a simple `RedisPubSub` instance:\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nconst pubsub = new RedisPubSub();\n```\n\nNow, implement your Subscriptions type resolver, using the `pubsub.asyncIterator` to map the event you need:\n\n```javascript\nconst SOMETHING_CHANGED_TOPIC = 'something_changed';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: () =\u003e pubsub.asyncIterator(SOMETHING_CHANGED_TOPIC),\n    },\n  },\n}\n```\n\n\u003e Subscriptions resolvers are not a function, but an object with `subscribe` method, that returns `AsyncIterable`.\n\nCalling the method `asyncIterator` of the `RedisPubSub` instance will send redis a `SUBSCRIBE` message to the topic provided and will return an `AsyncIterator` binded to the RedisPubSub instance and listens to any event published on that topic.\nNow, the GraphQL engine knows that `somethingChanged` is a subscription, and every time we will use `pubsub.publish` over this topic, the `RedisPubSub` will `PUBLISH` the event over redis to all other subscribed instances and those in their turn will emit the event to GraphQL using the `next` callback given by the GraphQL engine.\n\n```js\npubsub.publish(SOMETHING_CHANGED_TOPIC, { somethingChanged: { id: \"123\" }});\n```\n\n## Dynamically create a topic based on subscription args passed on the query\n\n```javascript\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: (_, args) =\u003e pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),\n    },\n  },\n}\n```\n\n## Using a pattern on subscription\n\n```javascript\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: (_, args) =\u003e pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}.*`, { pattern: true })\n    },\n  },\n}\n```\n\n## Using both arguments and payload to filter events\n\n```javascript\nimport { withFilter } from 'graphql-subscriptions';\n\nexport const resolvers = {\n  Subscription: {\n    somethingChanged: {\n      subscribe: withFilter(\n        (_, args) =\u003e pubsub.asyncIterator(`${SOMETHING_CHANGED_TOPIC}.${args.relevantId}`),\n        (payload, variables) =\u003e payload.somethingChanged.id === variables.relevantId,\n      ),\n    },\n  },\n}\n```\n\n## Configuring RedisPubSub\n\n`RedisPubSub` constructor can be passed a configuration object to enable some advanced features. \n\n```ts\nexport interface PubSubRedisOptions {\n  connection?: RedisOptions | string;\n  triggerTransform?: TriggerTransform;\n  connectionListener?: (err?: Error) =\u003e void;\n  publisher?: RedisClient;\n  subscriber?: RedisClient;\n  reviver?: Reviver;\n  serializer?: Serializer;\n  deserializer?: Deserializer;\n  messageEventName?: string;\n  pmessageEventName?: string;\n}\n```\n\n| option | type | default | description |\n| ------ | ---- | ------- | ----------- |\n| `connection` | [`object \\| string`](https://github.com/luin/ioredis#connect-to-redis) | `undefined` | the connection option is passed as is to the `ioredis` constructor to create redis subscriber and publisher instances. for greater controll, use `publisher` and `subscriber` options. |\n| `triggerTransform` | `function` | (trigger) =\u003e trigger | [deprecated](#using-trigger-transform-deprecated) |\n| `connectionListener` | `function` | `undefined` | pass in connection listener to log errors or make sure connection to redis instance was created successfully. |\n| `publisher` | `function` | `undefined` | must be passed along side `subscriber`. see [#creating-a-redis-client](#creating-a-redis-client) |\n| `subscriber` | `function` | `undefined` | must be passed along side `publisher`. see [#creating-a-redis-client](#creating-a-redis-client) |\n| `reviver` | `function` | `undefined` | see [#using-a-custom-reviver](#using-a-custom-reviver) |\n| `serializer` | `function` | `undefined` | see [#using-a-custom-serializerdeserializer](#using-a-custom-serializerdeserializer) |\n| `deserializer` | `function` | `undefined` | see [#using-a-custom-serializerdeserializer](#using-a-custom-serializerdeserializer) |\n| `messageEventName` | `string` | `undefined` | see [#receiving-messages-as-buffers](#receiving-messages-as-buffers) |\n| `pmessageEventName` | `string` | `undefined` | see [#receiving-messages-as-buffers](#receiving-messages-as-buffers) |\n\n## Creating a Redis Client\n\nThe basic usage is great for development and you will be able to connect to a Redis server running on your system seamlessly. For production usage, it is recommended to pass a redis client (like ioredis) to the RedisPubSub constructor. This way you can control all the options of your redis connection, for example the connection retry strategy.\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nimport * as Redis from 'ioredis';\n\nconst options = {\n  host: REDIS_DOMAIN_NAME,\n  port: PORT_NUMBER,\n  retryStrategy: times =\u003e {\n    // reconnect after\n    return Math.min(times * 50, 2000);\n  }\n};\n\nconst pubsub = new RedisPubSub({\n  ...,\n  publisher: new Redis(options),\n  subscriber: new Redis(options)\n});\n```\n\n### Receiving messages as Buffers\n\nSome Redis use cases require receiving binary-safe data back from redis (in a Buffer). To accomplish this, override the event names for receiving messages and pmessages.  Different redis clients use different names, for example:\n\n| library | message event | message event (Buffer) | pmessage event | pmessage event (Buffer) |\n| ------- | ------------- | ---------------------- | -------------- | ----------------------- |\n| ioredis | `message` | `messageBuffer` | `pmessage` | `pmessageBuffer` |\n| node-redis | `message` | `message_buffer` | `pmessage` | `pmessage_buffer` |\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nimport * as Redis from 'ioredis';\n\nconst pubsub = new RedisPubSub({\n  ...,\n  // Tells RedisPubSub to register callbacks on the messageBuffer and pmessageBuffer EventEmitters\n  messageEventName: 'messageBuffer',\n  pmessageEventName: 'pmessageBuffer',\n});\n```\n\n\n**Also works with your Redis Cluster**\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nimport { Cluster } from 'ioredis';\n\nconst cluster = new Cluster(REDIS_NODES); // like: [{host: 'ipOrHost', port: 1234}, ...]\nconst pubsub = new RedisPubSub({\n  ...,\n  publisher: cluster,\n  subscriber: cluster\n});\n```\n\n\nYou can learn more on the `ioredis` package [here](https://github.com/luin/ioredis).\n\n## Using a custom serializer/deserializer\n\nBy default, Javascript objects are (de)serialized using the `JSON.stringify` and `JSON.parse` methods.\nYou may pass your own serializer and/or deserializer function(s) as part of the options.\n\nThe `deserializer` will be called with an extra context object containing `pattern` (if available) and `channel` properties, allowing you to access this information when subscribing to a pattern.\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nimport { someSerializer, someDeserializer } from 'some-serializer-library';\n\nconst serialize = (source) =\u003e {\n  return someSerializer(source);\n};\n\nconst deserialize = (sourceOrBuffer, { channel, pattern }) =\u003e {\n  return someDeserializer(sourceOrBuffer, channel, pattern);\n};\n\nconst pubSub = new RedisPubSub({ ..., serializer: serialize, deserializer: deserialize });\n```\n\n## Using a custom reviver\n\nBy default, Javascript objects are serialized using the `JSON.stringify` and `JSON.parse` methods.\nThis means that not all objects - such as Date or Regexp objects - will deserialize correctly without a custom reviver, that work out of the box with the default in-memory implementation.\nFor handling such objects, you may pass your own reviver function to `JSON.parse`, for example to handle Date objects the following reviver can be used:\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\n\nconst dateReviver = (key, value) =\u003e {\n  const isISO8601Z = /^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2}(?:\\.\\d*)?)Z$/;\n  if (typeof value === 'string' \u0026\u0026 isISO8601Z.test(value)) {\n    const tempDateNumber = Date.parse(value);\n    if (!isNaN(tempDateNumber)) {\n      return new Date(tempDateNumber);\n    }\n  }\n  return value;\n};\n\nconst pubSub = new RedisPubSub({ ..., reviver: dateReviver });\n\npubSub.publish('Test', {\n  validTime: new Date(),\n  invalidTime: '2018-13-01T12:00:00Z'\n});\npubSub.subscribe('Test', message =\u003e {\n  message.validTime; // Javascript Date\n  message.invalidTime; // string\n});\n```\n\n## Old Usage (Deprecated)\n\n```javascript\nimport { RedisPubSub } from 'graphql-redis-subscriptions';\nconst pubsub = new RedisPubSub();\nconst subscriptionManager = new SubscriptionManager({\n  schema,\n  pubsub,\n  setupFunctions: {},\n});\n```\n\n## Using Trigger Transform (Deprecated)\n\nRecently, graphql-subscriptions package added a way to pass in options to each call of subscribe.\nThose options are constructed via the setupFunctions object you provide the Subscription Manager constructor.\nThe reason for graphql-subscriptions to add that feature is to allow pub sub engines a way to reduce their subscription set using the best method of said engine.\nFor example, Meteor's live query could use Mongo selector with arguments passed from the subscription like the subscribed entity id.\nFor Redis, this could be a bit more simplified, but much more generic.\nThe standard for Redis subscriptions is to use dot notations to make the subscription more specific.\nThis is only the standard but I would like to present an example of creating a specific subscription using the channel options feature.\n\nFirst I create a simple and generic trigger transform \n```javascript\nconst triggerTransform = (trigger, {path}) =\u003e [trigger, ...path].join('.');\n```\n\nThen I pass it to the `RedisPubSub` constructor.\n```javascript\nconst pubsub = new RedisPubSub({\n  triggerTransform,\n});\n```\nLastly, I provide a setupFunction for `commentsAdded` subscription field.\nIt specifies one trigger called `comments.added` and it is called with the channelOptions object that holds `repoName` path fragment.\n```javascript\nconst subscriptionManager = new SubscriptionManager({\n  schema,\n  setupFunctions: {\n    commentsAdded: (options, {repoName}) =\u003e ({\n      'comments.added': {\n        channelOptions: {path: [repoName]},\n      },\n    }),\n  },\n  pubsub,\n});\n```\n\nWhen I call `subscribe` like this:\n```javascript\nconst query = `\n  subscription X($repoName: String!) {\n    commentsAdded(repoName: $repoName)\n  }\n`;\nconst variables = {repoName: 'graphql-redis-subscriptions'};\nsubscriptionManager.subscribe({query, operationName: 'X', variables, callback});\n```\n\nThe subscription string that Redis will receive will be `comments.added.graphql-redis-subscriptions`.\nThis subscription string is much more specific and means the the filtering required for this type of subscription is not needed anymore.\nThis is one step towards lifting the load off of the GraphQL API server regarding subscriptions.\n\n## Publishing New Versions\n\nThis package uses GitHub Actions for automated publishing. To publish a new version:\n\n1. Go to the GitHub repository\n2. Click \"Releases\" in the right sidebar\n3. Click \"Draft a new release\"\n4. Choose or create a new tag (e.g., v2.7.1)\n5. Fill in the release title and description\n6. Click \"Publish release\"\n\nThe GitHub Action will automatically:\n- Run tests\n- Build the package\n- Publish to npm with provenance\n\nYou can verify the published version on [npm](https://www.npmjs.com/package/graphql-redis-subscriptions).\n\n## Tests\n\n### Spin a Redis in docker server and cluster\nPlease refer to https://github.com/Grokzen/docker-redis-cluster documentation to start a cluster\n```shell script\n$ docker run --rm -p 6379:6379 redis:alpine\n$ export REDIS_CLUSTER_IP=0.0.0.0; docker run -e \"IP=0.0.0.0\" --rm -p 7006:7000 -p 7001:7001 -p 7002:7002 -p 7003:7003 -p 7004:7004 -p 7005:7005 grokzen/redis-cluster\n```\n\n### Test\n```shell script\nnpm run test\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidyaha%2Fgraphql-redis-subscriptions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidyaha%2Fgraphql-redis-subscriptions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidyaha%2Fgraphql-redis-subscriptions/lists"}