{"_id":"@chat1/fastify-sse-v2","name":"@chat1/fastify-sse-v2","dist-tags":{"latest":"3.1.0"},"versions":{"3.1.0":{"name":"@chat1/fastify-sse-v2","version":"3.1.0","description":"Fastify plugin for sending server side events.","main":"lib/index.js","types":"lib/index.d.ts","scripts":{"test":"mocha -r ts-node/register test/**/*.spec.ts","clean":"rm -rf lib","lint":"eslint src/","check-types":"tsc --noEmit","prepublishOnly":"yarn run build","prebuild":"yarn run clean","build":"tsc -p tsconfig.build.json --outDir lib"},"repository":{"type":"git","url":"git+https://github.com/nodefactoryio/fastify-sse-v2.git"},"author":{"name":"Marin Petrunić","email":"marin@nodefactory.io"},"license":"MIT","bugs":{"url":"https://github.com/nodefactoryio/fastify-sse-v2/issues"},"homepage":"https://github.com/nodefactoryio/fastify-sse-v2#readme","devDependencies":{"@chainsafe/eslint-config":"^1.1.0","@rushstack/eslint-patch":"^1.2.0","@types/chai":"^4.1.7","@types/eventsource":"^1.1.2","@types/mocha":"^10.0.1","@types/node":"^16.0.0","@types/sinon":"^9.0.0","chai":"^4.2.0","eslint":"8.28.0","eventsource":"^1.0.7","fastify":"^4.10.2","mocha":"^10.2.0","sinon":"^9.0.2","ts-node":"^8.3.0","typescript":"~4.7"},"dependencies":{"fastify-plugin":"^4.3.0","it-pushable":"^1.4.2","it-to-stream":"^1.0.0"},"peerDependencies":{"fastify":">=4"},"gitHead":"d4d10b0f3f0e654f460ae9862c2e16f75eca30d4","_id":"@chat1/fastify-sse-v2@3.1.0","_nodeVersion":"16.18.0","_npmVersion":"8.19.2","dist":{"integrity":"sha512-5ZWgaIfwNE8DT6SCxqKy6seo8/LYsikZIzOQZwfskUuZrOsVEr9usMhDPSjMAQvgI7IsjSw2MjWnjJwgyP9hJg==","shasum":"cb3b8a033e78d8eaafd7ac50e97727a885b72d91","tarball":"https://registry.npmjs.org/@chat1/fastify-sse-v2/-/fastify-sse-v2-3.1.0.tgz","fileCount":19,"unpackedSize":18870,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCwzn+9RkGzPc7jRERYID0pGR4Z3qQUIy5jI4QV/ocv0QIgSxa4j+a/d4mcCmi7PhAzJwi0VKK3gR5zQivxEXi8kkY="}]},"_npmUser":{"name":"shiyue.job","email":"shiyue.job@gmail.com"},"directories":{},"maintainers":[{"name":"shiyue.job","email":"shiyue.job@gmail.com"},{"name":"angelise7","email":"249749871@qq.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fastify-sse-v2_3.1.0_1685005965040_0.05599141156666465"},"_hasShrinkwrap":false}},"time":{"created":"2023-05-25T09:12:44.984Z","3.1.0":"2023-05-25T09:12:45.205Z","modified":"2023-05-25T09:12:45.386Z"},"maintainers":[{"name":"shiyue.job","email":"shiyue.job@gmail.com"},{"name":"angelise7","email":"249749871@qq.com"}],"description":"Fastify plugin for sending server side events.","homepage":"https://github.com/nodefactoryio/fastify-sse-v2#readme","repository":{"type":"git","url":"git+https://github.com/nodefactoryio/fastify-sse-v2.git"},"author":{"name":"Marin Petrunić","email":"marin@nodefactory.io"},"bugs":{"url":"https://github.com/nodefactoryio/fastify-sse-v2/issues"},"license":"MIT","readme":"# Fastify SSE Plugin\n![CI check](https://github.com/NodeFactoryIo/fastify-sse-v2/workflows/CI%20check/badge.svg?branch=master)\n[![npm version](https://badge.fury.io/js/fastify-sse-v2.svg)](https://badge.fury.io/js/fastify-sse-v2)\n\nFastify plugin for sending [Server-sent events](https://en.wikipedia.org/wiki/Server-sent_events).\n\nFor `fastify@2.x` use [fastify-sse-v2@1.x](https://github.com/NodeFactoryIo/fastify-sse-v2/tree/1.x)!\n\n### How to use?\n\n```terminal\nyarn add fastify-sse-v2\n```\nRegister fastify-sse-v2 plugin into your fastify instance:\n```javascript\nimport {FastifySSEPlugin} from \"fastify-sse-v2\";\n\nconst server = fastify();\nserver.register(FastifySSEPlugin);\n```\n\n#### Sending events from AsyncIterable source\n\n```javascript\nimport {FastifySSEPlugin} from \"fastify-sse-v2\";\n\nconst server = fastify();\nserver.register(FastifySSEPlugin);\n\nserver.get(\"/\", function (req, res) {\n    res.sse((async function * source () {\n          for (let i = 0; i < 10; i++) {\n            sleep(2000);\n            yield {id: String(i), data: \"Some message\"};\n          }\n    })());\n});\n```\n#### Sending individual events\n\n```javascript\nimport {FastifySSEPlugin} from \"fastify-sse-v2\";\n\nconst server = fastify();\nserver.register(FastifySSEPlugin);\n\nserver.get(\"/\", async function (req, res) {\n    for (let i = 0; i < 10; i++) {\n      await sleep(2000);\n      res.sse({id: String(i), data: \"Some message\"});\n    }\n});\n\nfastify.get('/listenForChanges', {}, (request, reply) => {\n    const listenStream = fastify.db.watch('doc-uuid')\n        .on('data', (data)=>reply.sse({ data: JSON.stringify(data) }))\n        .on('delete', () => reply.sse({ event: 'close' }))\n    request.socket.on('close', ()=>listenStream.end())\n})\n```\n\n##### Sending events from EventEmmiters\n\n* [not supported in all nodejs versions](https://nodejs.org/api/events.html#events_events_on_emitter_eventname_options)\n\n```javascript\nimport {FastifySSEPlugin} from \"fastify-sse-v2\";\nimport {on} from \"events\";\n\nconst server = fastify();\nserver.register(FastifySSEPlugin);\n\nserver.get(\"/\", function (req, res) {\n    res.sse(\n  (async function* () {\n    for await (const [event] of on(eventEmmitter, \"update\")) {\n      yield {\n        event: event.name,\n        data: JSON.stringify(event),\n      };\n    }\n  })()\n);\n});\n```\n\n\n##### Note\n- to remove event listeners (or some other cleanup) when client closes connection,\n you can listen on connection closing event: `request.socket.on('close', () => abortController.abort());\n`\n\n","readmeFilename":"README.md"}