{"_id":"@ananay-nag/fastify-socket-io","name":"@ananay-nag/fastify-socket-io","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@ananay-nag/fastify-socket-io","version":"1.0.0","description":"Fastify plugin for Socket.IO","main":"dist/index.js","module":"dist/index.mjs","exports":{".":{"types":"./types/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"types":"types/index.d.ts","author":{"name":"Ananay Nag"},"private":false,"type":"commonjs","scripts":{"build":"tsup index.js --format cjs,esm --clean --minify","release":"npm publish --access public","prepublishOnly":"npm run build && npm run test","lint":"standard","unit":"node --test","test":"npm run lint && npm run unit && npm run types","types":"tsc --noEmit","coverage":"c8 node --test"},"keywords":["fastify","plugin","socket.io","websocket","realtime"],"license":"MIT","engines":{"node":">=20"},"peerDependencies":{"fastify":"^5.0.0","socket.io":"^4.8.0"},"dependencies":{"@fastify/error":"^4.2.0","fastify-plugin":"^5.0.1"},"devDependencies":{"@fastify/jwt":"^10.1.0","@types/node":"^22.15.30","c8":"^10.1.3","fastify":"^5.3.3","socket.io":"^4.8.1","socket.io-client":"^4.8.1","standard":"^17.1.2","tsup":"^6.5.0","typescript":"^5.8.3"},"gitHead":"9e9852c3b9315d9d743e223aebf1ffc94616cdad","_id":"@ananay-nag/fastify-socket-io@1.0.0","_nodeVersion":"24.16.0","_npmVersion":"11.13.0","dist":{"integrity":"sha512-A/zbmiwM87+8+p+Fvltyjric2nr8ckZdYp+NZasQSnNdkFKrYKLeWhttYcjMAl2poRozJsokF1JPITrMfjrK7A==","shasum":"99d33e3bfbb38e4a73677c1640691195255a2674","tarball":"https://registry.npmjs.org/@ananay-nag/fastify-socket-io/-/fastify-socket-io-1.0.0.tgz","fileCount":7,"unpackedSize":16020,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIA814vea+q+dbuO1MsczDTuGU16q/6QDx8+6kHovxeidAiAwlY7HqT88xsvNkn6HFhY2CTm3CKTZ6/sjyW2pAGzSjg=="}]},"_npmUser":{"name":"ananay-nag","email":"ananaynag1994s@gmail.com"},"directories":{},"maintainers":[{"name":"ananay-nag","email":"ananaynag1994s@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/fastify-socket-io_1.0.0_1781607719780_0.39474335064598565"},"_hasShrinkwrap":false}},"time":{"created":"2026-06-16T11:01:59.558Z","1.0.0":"2026-06-16T11:01:59.949Z","modified":"2026-06-16T11:02:00.258Z"},"maintainers":[{"name":"ananay-nag","email":"ananaynag1994s@gmail.com"}],"description":"Fastify plugin for Socket.IO","keywords":["fastify","plugin","socket.io","websocket","realtime"],"author":{"name":"Ananay Nag"},"license":"MIT","readme":"# @ananay-nag/fastify-socket-io\n\nFastify plugin for integrating Socket.IO with a Fastify server.\n\nThis plugin follows Fastify's plugin model based on `register`, encapsulation, decorators, and lifecycle hooks, and it uses `fastify-plugin` metadata for compatibility and naming.\nSocket.IO exposes server options such as `path`, `serveClient`, `adapter`, `connectTimeout`, `pingTimeout`, `pingInterval`, `maxHttpBufferSize`, `transports`, `perMessageDeflate`, `httpCompression`, `cors`, `cookie`, and `allowEIO3`, and this plugin forwards those options to the underlying `Server` instance.\n\n## Install\n\n```bash\nnpm i @ananay-nag/fastify-socket-io socket.io\n```\n\n## Usage\n\n```js\nconst fastify = require('fastify')()\n\nfastify.register(require('@ananay-nag/fastify-socket-io'), {\n  cors: {\n    origin: ['https://example.com'],\n    credentials: true\n  },\n  connectionHandler (socket) {\n    socket.emit('hello', 'world')\n  }\n})\n\nfastify.listen({ port: 3000 })\n```\n\n## Features\n\n- Decorates Fastify with `fastify.io` and `fastify.socketIO`.\n- Supports custom decorator names.\n- Supports namespace decoration and eager namespace registration.\n- Supports root and namespace Socket.IO middlewares.\n- Supports graceful shutdown through `onClose`.\n- **Connection State Recovery**: Temporarily stores messages when a client disconnects and restores them upon reconnection.\n- **Fastify JWT Authentication Hook**: Natively validates tokens using `@fastify/jwt` globally before standard middlewares.\n- **Strongly Typed Events (TypeScript)**: Strictly type events for emission, listening, server-side, and Socket data using built-in generics.\n- Includes TypeScript declarations and integration tests.\n\n## Advanced Usage\n\n### JWT Authentication\n\nIntegrates flawlessly with `@fastify/jwt`. When `jwtAuth` is enabled, the plugin will look for the JWT in the Socket.IO `auth.token` payload or `Authorization: Bearer <token>` header, and attach the verified payload to `socket.data.user`.\n\n```js\nconst fastify = require('fastify')()\n\nfastify.register(require('@fastify/jwt'), { secret: 'supersecret' })\n\nfastify.register(require('@ananay-nag/fastify-socket-io'), {\n  jwtAuth: true,\n  connectionHandler (socket) {\n    fastify.log.info('Authenticated user:', socket.data.user)\n  }\n})\n```\n\n### Connection State Recovery\n\nEasily enable Socket.IO's [Connection State Recovery](https://socket.io/docs/v4/connection-state-recovery).\n\n```js\nfastify.register(require('@ananay-nag/fastify-socket-io'), {\n  connectionStateRecovery: true // uses optimal defaults\n})\n```\n\n### Strongly Typed Events (TypeScript)\n\nFull intellisense and compiler safety for your sockets.\n\n```typescript\nimport Fastify from 'fastify'\nimport fastifySocketIO from '@ananay-nag/fastify-socket-io'\n\ninterface ClientToServerEvents {\n  hello: () => void;\n}\n\ninterface ServerToClientEvents {\n  noArg: () => void;\n  basicEmit: (a: number, b: string, c: Buffer) => void;\n}\n\ninterface InterServerEvents {\n  ping: () => void;\n}\n\ninterface SocketData {\n  user: { id: string };\n}\n\nconst fastify = Fastify()\n\n// fastify.io is strictly typed based on these generics!\nfastify.register(fastifySocketIO<\n  ClientToServerEvents,\n  ServerToClientEvents,\n  InterServerEvents,\n  SocketData\n>)\n```\n\n## License\n\nMIT\n","readmeFilename":"README.md","_rev":"1-15f7ba6dd8001e559ff48daa44003427"}