{"id":28898846,"url":"https://github.com/ofek29/fastify-iovalkey","last_synced_at":"2026-04-30T02:35:00.299Z","repository":{"id":291541152,"uuid":"977924786","full_name":"ofek29/fastify-iovalkey","owner":"ofek29","description":"Plugin to share Valkey connection across Fastify","archived":false,"fork":false,"pushed_at":"2025-05-24T13:09:41.000Z","size":32,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-06-14T12:24:57.993Z","etag":null,"topics":["caching","fastify","fastify-plugin","iovalkey","valkey"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@ofek.a/fastify-iovalkey","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/ofek29.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":"2025-05-05T07:35:15.000Z","updated_at":"2025-05-24T14:01:41.000Z","dependencies_parsed_at":"2025-05-05T09:29:00.964Z","dependency_job_id":"6813bbf9-4956-4f31-b2c5-296593fec484","html_url":"https://github.com/ofek29/fastify-iovalkey","commit_stats":null,"previous_names":["ofek29/fastify-iovalkey"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ofek29/fastify-iovalkey","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofek29%2Ffastify-iovalkey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofek29%2Ffastify-iovalkey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofek29%2Ffastify-iovalkey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofek29%2Ffastify-iovalkey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ofek29","download_url":"https://codeload.github.com/ofek29/fastify-iovalkey/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ofek29%2Ffastify-iovalkey/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261088330,"owners_count":23107676,"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","iovalkey","valkey"],"created_at":"2025-06-21T08:00:30.064Z","updated_at":"2026-04-30T02:35:00.292Z","avatar_url":"https://github.com/ofek29.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @ofek.a/fastify-iovalkey\n\nFastify Valkey connection plugin, with this you can share the same Valkey connection in every part of your server.\n\nUnder the hood [iovalkey](https://github.com/valkey-io/iovalkey) is used as client.\n\n## Install\n\n```\nnpm i @ofek.a/fastify-iovalkey\n```\n\n### Compatibility\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `0.x`        | `^5.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 Valkey Client\n\nthe `options` that you pass to `register` will be passed to the Valkey client.\n\n```js\nimport Fastify from 'fastify'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify()\n\n// create by specifying host\nawait fastify.register(fastifyValkey, { host: '127.0.0.1' })\n\n// OR by specifying Valkey URL\nawait fastify.register(fastifyValkey, { url: 'valkey://127.0.0.1', /* other valkey options */ })\n\n// OR with more options\nawait fastify.register(fastifyValkey, {\n  host: '127.0.0.1',\n  password: '***',\n  port: 6379, // Valkey port\n  family: 4   // 4 (IPv4) or 6 (IPv6)\n})\n```\n\n### Accessing the Valkey Client\n\nOnce you have registered your plugin, you can access the Valkey client via `fastify.iovalkey`.\n\nThe client is automatically closed when the fastify instance is closed.\n\n```js\nimport Fastify from 'fastify'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify({ logger: true })\n\nawait fastify.register(fastifyValkey, {\n  host: '127.0.0.1',\n  password: 'your strong password here',\n  port: 6379, // Valkey port\n  family: 4   // 4 (IPv4) or 6 (IPv6)\n})\n\nfastify.get('/foo', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  try {\n    const val = await iovalkey.get(req.query.key)\n    return val\n  } catch (err) {\n    return err\n  }\n})\n\nfastify.post('/foo', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  try {\n    await iovalkey.set(req.body.key, req.body.value)\n    return { status: 'ok' }\n  } catch (err) {\n    return err\n  }\n})\n\ntry {\n  await fastify.listen({ port: 3000 })\n  console.log(`server listening on ${fastify.server.address().port}`)\n} catch (err) {\n  fastify.log.error(err)\n  process.exit(1)\n}\n```\n\n### Using an existing Valkey client\n\nYou may also supply an existing *Valkey* 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\nimport Fastify from 'fastify'\nimport Valkey from 'iovalkey'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify()\nconst client = new Valkey({ host: 'localhost', port: 6379 })\n\nawait fastify.register(fastifyValkey, { client })\n```\n\nYou can also supply a *Valkey Cluster* instance to the client:\n\n```js\nimport Fastify from 'fastify'\nimport Valkey from 'iovalkey'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify()\nconst client = new Valkey.Cluster([{ host: 'localhost', port: 6379 }]);\n\nawait fastify.register(fastifyValkey, { client })\n```\n\nNote: by default, *@ofek.a/fastify-iovalkey* will **not** automatically close the client\nconnection when the Fastify server shuts down.\n\nTo automatically close the client connection, set closeClient to true.\n\n```js\nawait fastify.register(fastifyValkey, { client, closeClient: true })\n```\n\n## Registering multiple Valkey client instances\n\nBy using the `namespace` option you can register multiple Valkey client instances.\n\n```js\nimport Fastify from 'fastify'\nimport Valkey from 'iovalkey'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify()\nconst valkeyClient = new Valkey({ host: 'localhost', port: 6379 })\n\nawait fastify.register(fastifyValkey, {\n  host: '127.0.0.1',\n  port: 6380,\n  namespace: 'hello'\n})\n\nawait fastify.register(fastifyValkey, {\n  client: valkeyClient,\n  namespace: 'world'\n})\n\n// Here we will use the `hello` named instance\nfastify.get('/hello', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  \n  try {\n    const val = await iovalkey.hello.get(req.query.key)\n    return val\n  } catch (err) {\n    return err\n  }\n})\n\nfastify.post('/hello', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  \n  try {\n    await iovalkey['hello'].set(req.body.key, req.body.value)\n    return { status: 'ok' }\n  } catch (err) {\n    return err\n  }\n})\n\n// Here we will use the `world` named instance\nfastify.get('/world', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  \n  try {\n    const val = await iovalkey['world'].get(req.query.key)\n    return val\n  } catch (err) {\n    return err\n  }\n})\n\nfastify.post('/world', async (req, reply) =\u003e {\n  const { iovalkey } = fastify\n  \n  try {\n    await iovalkey.world.set(req.body.key, req.body.value)\n    return { status: 'ok' }\n  } catch (err) {\n    return err\n  }\n})\n\ntry {\n  await fastify.listen({ port: 3000 })\n  console.log(`server listening on ${fastify.server.address().port}`)\n} catch (err) {\n  fastify.log.error(err)\n  process.exit(1)\n}\n```\n\n## Valkey streams (Valkey 5.0 or greater is required)\n\n`@fastify/iovalkey` supports Valkey streams out of the box.\n\n```js\nimport Fastify from 'fastify'\nimport fastifyValkey from '@ofek.a/fastify-iovalkey'\n\nconst fastify = Fastify()\n\nawait fastify.register(fastifyValkey, {\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.iovalkey.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 valkeyStream = await fastify.iovalkey.xread(['STREAMS', 'my awesome fastify stream name', 0])\n\n  // We parse the results\n  let response = []\n  let events = valkeyStream[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  return { output: response }\n  // Will return something like this :\n  // { \"output\": [\"#LOG: id is 1559985742035-0\", \"hello\", \"fastify is awesome\"] }\n})\n\ntry {\n  await fastify.listen({ port: 3000 })\n  console.log(`server listening on ${fastify.server.address().port}`)\n} catch (err) {\n  fastify.log.error(err)\n  process.exit(1)\n}\n```\n*NB you can find more information about Valkey streams and the relevant commands [here](https://valkey.io/topics/streams-intro/) and [here](https://valkey.io/commands/#stream).*\n\n## Valkey connection error\nThe majority of errors are silent due to the `iovalkey` silent error handling but during the plugin registration it will check that the connection with the valkey instance is correctly established.\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\n## License\n\nLicensed under [MIT](./LICENSE).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofek29%2Ffastify-iovalkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fofek29%2Ffastify-iovalkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fofek29%2Ffastify-iovalkey/lists"}