{"id":13524669,"url":"https://github.com/fastify/fastify-redis","last_synced_at":"2025-05-15T02:09:05.192Z","repository":{"id":19538894,"uuid":"87282371","full_name":"fastify/fastify-redis","owner":"fastify","description":"Plugin to share a common Redis connection across Fastify","archived":false,"fork":false,"pushed_at":"2025-05-01T18:51:04.000Z","size":186,"stargazers_count":215,"open_issues_count":1,"forks_count":34,"subscribers_count":20,"default_branch":"main","last_synced_at":"2025-05-01T19:42:02.759Z","etag":null,"topics":["caching","fastify","fastify-plugin","redis"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/redis","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/fastify.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},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2017-04-05T07:55:56.000Z","updated_at":"2025-05-01T18:51:01.000Z","dependencies_parsed_at":"2023-01-13T20:26:23.838Z","dependency_job_id":"7299297b-4551-4722-9472-b3e453e83807","html_url":"https://github.com/fastify/fastify-redis","commit_stats":{"total_commits":189,"total_committers":30,"mean_commits":6.3,"dds":0.7407407407407407,"last_synced_commit":"bddd3d9a1ebc5e49e083707c8940212ffd776a7e"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-redis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-redis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-redis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-redis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-redis/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254259384,"owners_count":22040820,"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":["caching","fastify","fastify-plugin","redis"],"created_at":"2024-08-01T06:01:12.317Z","updated_at":"2025-05-15T02:09:00.179Z","avatar_url":"https://github.com/fastify.png","language":"JavaScript","readme":"# @fastify/redis\n\n[![CI](https://github.com/fastify/fastify-redis/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-redis/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/redis.svg?style=flat)](https://www.npmjs.com/package/@fastify/redis)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nFastify Redis connection plugin; with this you can share the same Redis connection in every part of your server.\n\n## Install\n\n```\nnpm i @fastify/redis\n```\n\n### Compatibility\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `\u003e=7.x`        | `^5.x`          |\n| `^6.x`         | `^4.x`          |\n| `\u003e=4.x \u003c6.x`   | `^3.x`          |\n| `^3.x`         | `^2.x`          |\n| `\u003e=1.x \u003c3.x`   | `^1.x`          |\n\n\nPlease note that if a Fastify version is out of support, then so are the corresponding versions of this plugin\nin the table above.\nSee [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.\n\n## Usage\n\nAdd it to your project with `register` and you are done!\n\n### Create a new Redis Client\n\nUnder the hood [ioredis](https://github.com/luin/ioredis) is used as client, the ``options`` that you pass to `register` will be passed to the Redis client.\n\n```js\nconst fastify = require('fastify')()\n\n// create by specifying host\nfastify.register(require('@fastify/redis'), { host: '127.0.0.1' })\n\n// OR by specifying Redis URL\nfastify.register(require('@fastify/redis'), { url: 'redis://127.0.0.1', /* other redis options */ })\n\n// OR with more options\nfastify.register(require('@fastify/redis'), {\n  host: '127.0.0.1',\n  password: '***',\n  port: 6379, // Redis port\n  family: 4   // 4 (IPv4) or 6 (IPv6)\n})\n```\n\n### Accessing the Redis Client\n\nOnce you have registered your plugin, you can access the Redis client via `fastify.redis`.\n\nThe client is automatically closed when the fastify instance is closed.\n\n```js\n'use strict'\n\nconst Fastify = require('fastify')\nconst fastifyRedis = require('@fastify/redis')\n\nconst fastify = Fastify({ logger: true })\n\nfastify.register(fastifyRedis, {\n  host: '127.0.0.1',\n  password: 'your strong password here',\n  port: 6379, // Redis port\n  family: 4   // 4 (IPv4) or 6 (IPv6)\n})\n\nfastify.get('/foo', (req, reply) =\u003e {\n  const { redis } = fastify\n  redis.get(req.query.key, (err, val) =\u003e {\n    reply.send(err || val)\n  })\n})\n\nfastify.post('/foo', (req, reply) =\u003e {\n  const { redis } = fastify\n  redis.set(req.body.key, req.body.value, (err) =\u003e {\n    reply.send(err || { status: 'ok' })\n  })\n})\n\nfastify.listen({ port: 3000 }, err =\u003e {\n  if (err) throw err\n  console.log(`server listening on ${fastify.server.address().port}`)\n})\n```\n\n### Using an existing Redis client\n\nYou may also supply an existing *Redis* client instance by passing an options\nobject with the `client` property set to the instance. In this case,\nthe client is not automatically closed when the Fastify instance is\nclosed.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\nconst Redis = require('ioredis')\n\nconst client = new Redis({ host: 'localhost', port: 6379 })\n\nfastify.register(require('@fastify/redis'), { client })\n```\n\nNote: by default, *@fastify/redis* will **not** automatically close the client\nconnection when the Fastify server shuts down.\n\nTo automatically close the client connection, set clientClose to true.\n\n```js\nfastify.register(require('@fastify/redis'), { client, closeClient: true })\n```\n\n## Registering multiple Redis client instances\n\nBy using the `namespace` option you can register multiple Redis client instances.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nfastify\n  .register(require('@fastify/redis'), {\n    host: '127.0.0.1',\n    port: 6380,\n    namespace: 'hello'\n  })\n  .register(require('@fastify/redis'), {\n    client: redis,\n    namespace: 'world'\n  })\n\n// Here we will use the `hello` named instance\nfastify.get('/hello', (req, reply) =\u003e {\n  const { redis } = fastify\n\n  redis.hello.get(req.query.key, (err, val) =\u003e {\n    reply.send(err || val)\n  })\n})\n\nfastify.post('/hello', (req, reply) =\u003e {\n  const { redis } = fastify\n\n  redis['hello'].set(req.body.key, req.body.value, (err) =\u003e {\n    reply.send(err || { status: 'ok' })\n  })\n})\n\n// Here we will use the `world` named instance\nfastify.get('/world', (req, reply) =\u003e {\n  const { redis } = fastify\n\n  redis['world'].get(req.query.key, (err, val) =\u003e {\n    reply.send(err || val)\n  })\n})\n\nfastify.post('/world', (req, reply) =\u003e {\n  const { redis } = fastify\n\n  redis.world.set(req.body.key, req.body.value, (err) =\u003e {\n    reply.send(err || { status: 'ok' })\n  })\n})\n\nfastify.listen({ port: 3000 }, function (err) {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n\n```\n\n## Redis streams (Redis 5.0 or greater is required)\n\n`@fastify/redis` supports Redis streams out of the box.\n\n```js\n'use strict'\n\nconst fastify = require('fastify')()\n\nfastify.register(require('@fastify/redis'), {\n  host: '127.0.0.1',\n  port: 6380\n})\n\nfastify.get('/streams', async (request, reply) =\u003e {\n  // We write an event to the stream 'my awesome fastify stream name', setting 'key' to 'value'\n  await fastify.redis.xadd(['my awesome fastify stream name', '*', 'hello', 'fastify is awesome'])\n\n  // We read events from the beginning of the stream called 'my awesome fastify stream name'\n  let redisStream = await fastify.redis.xread(['STREAMS', 'my awesome fastify stream name', 0])\n\n  // We parse the results\n  let response = []\n  let events = redisStream[0][1]\n\n  for (let i = 0; i \u003c events.length; i++) {\n    const e = events[i]\n    response.push(`#LOG: id is ${e[0].toString()}`)\n\n    // We log each key\n    for (const key in e[1]) {\n      response.push(e[1][key].toString())\n    }\n  }\n\n  reply.status(200)\n  return { output: response }\n  // Will return something like this :\n  // { \"output\": [\"#LOG: id is 1559985742035-0\", \"hello\", \"fastify is awesome\"] }\n})\n\nfastify.listen({ port: 3000 }, function (err) {\n  if (err) {\n    fastify.log.error(err)\n    process.exit(1)\n  }\n})\n```\n*NB you can find more information about Redis streams and the relevant commands [here](https://redis.io/topics/streams-intro) and [here](https://redis.io/commands#stream).*\n\n## Redis connection error\nThe majority of errors are silent due to the `ioredis` silent error handling but during the plugin registration it will check that the connection with the redis instance is correctly estabilished.\nIn this case, you can receive an `ERR_AVVIO_PLUGIN_TIMEOUT` error if the connection cannot be established in the expected time frame or a dedicated error for an invalid connection.\n\n## Acknowledgments\n\nThis project is kindly sponsored by:\n- [nearForm](https://nearform.com)\n- [LetzDoIt](https://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["JavaScript","\u003ch2 align=\"center\"\u003eAwesome Fastify\u003c/h2\u003e"],"sub_categories":["\u003ch2 align=\"center\"\u003eEcosystem\u003c/h2\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-redis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-redis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-redis/lists"}