{"id":14978174,"url":"https://github.com/socketio/socket.io-sticky","last_synced_at":"2025-10-19T11:30:43.758Z","repository":{"id":44577313,"uuid":"332382319","full_name":"socketio/socket.io-sticky","owner":"socketio","description":"A simple and performant way to use Socket.IO within a cluster.","archived":false,"fork":false,"pushed_at":"2023-08-14T07:44:49.000Z","size":161,"stargazers_count":45,"open_issues_count":5,"forks_count":14,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-01-27T19:37:57.491Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://socket.io","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/socketio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2021-01-24T06:26:16.000Z","updated_at":"2025-01-08T23:41:45.000Z","dependencies_parsed_at":"2024-01-23T21:46:47.980Z","dependency_job_id":null,"html_url":"https://github.com/socketio/socket.io-sticky","commit_stats":{"total_commits":20,"total_committers":1,"mean_commits":20.0,"dds":0.0,"last_synced_commit":"518bd0b989e20fadecbbb6b225578bfca82ad963"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-sticky","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-sticky/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-sticky/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-sticky/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketio","download_url":"https://codeload.github.com/socketio/socket.io-sticky/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237116688,"owners_count":19258299,"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":[],"created_at":"2024-09-24T13:56:59.267Z","updated_at":"2025-10-19T11:30:43.355Z","avatar_url":"https://github.com/socketio.png","language":"JavaScript","readme":"# Sticky sessions for Socket.IO\n\nA simple and performant way to use [Socket.IO](https://socket.io/) within a [cluster](http://nodejs.org/docs/latest/api/cluster.html).\n\nUnlike other packages like [sticky-session](https://github.com/indutny/sticky-session), the routing is based on the session ID (the `sid` query parameter).\n\n![Cluster diagram](./assets/socket.io-cluster.png)\n\nSee also:\n\n- [sticky-session](https://github.com/indutny/sticky-session) (routing based on `connection.remoteAddress`)\n- [socketio-sticky-session](https://github.com/wzrdtales/socket-io-sticky-session) (routing based on the `x-forwarded-for` header)\n\n**Table of contents**\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [How it works](#how-it-works)\n- [Notes](#notes)\n- [License](#license)\n\n## Installation\n\n```\nnpm install @socket.io/sticky\n```\n\n## Usage\n\n```js\nconst cluster = require(\"cluster\");\nconst http = require(\"http\");\nconst { Server } = require(\"socket.io\");\nconst numCPUs = require(\"os\").cpus().length;\nconst { setupMaster, setupWorker } = require(\"@socket.io/sticky\");\nconst { createAdapter, setupPrimary } = require(\"@socket.io/cluster-adapter\");\n\nif (cluster.isMaster) {\n  console.log(`Master ${process.pid} is running`);\n\n  const httpServer = http.createServer();\n\n  setupMaster(httpServer, {\n    loadBalancingMethod: \"least-connection\", // either \"random\", \"round-robin\" or \"least-connection\"\n  });\n\n  setupPrimary();\n\n  httpServer.listen(3000);\n\n  for (let i = 0; i \u003c numCPUs; i++) {\n    cluster.fork();\n  }\n\n  cluster.on(\"exit\", (worker) =\u003e {\n    console.log(`Worker ${worker.process.pid} died`);\n    cluster.fork();\n  });\n} else {\n  console.log(`Worker ${process.pid} started`);\n\n  const httpServer = http.createServer();\n  const io = new Server(httpServer);\n  io.adapter(createAdapter());\n  setupWorker(io);\n\n  io.on(\"connection\", (socket) =\u003e {\n    /* ... */\n  });\n}\n```\n\n## How it works\n\nThe first HTTP request (without `sid` query parameter) is forwarded to a random worker (based on the `loadBalancingMethod` option).\n\nThe underlying Engine.IO server creates a new session and emits a `connection` event with the session ID. The worker sends this session ID to the master, which stores the relationship between the worker ID and the session ID.\n\nFor subsequent requests, the `sid` query parameter is extracted by the master process, which then forwards the handle to the right worker.\n\n## Notes\n\n- this package is not needed if you only use WebSockets (which might be a sensible choice as of 2021)\n\n```js\n// client-side\nconst socket = io({\n  transports: [\"websocket\"] // HTTP long-polling is disabled\n});\n```\n\n- in a multi-server setup, you will need to use another adapter, like the [Redis adapter](https://socket.io/docs/v4/redis-adapter/)\n\n![Cluster diagram with Redis](./assets/socket.io-cluster-redis.png)\n\n- this module is not compatible with an HTTPS server\n\nFor more information, please see [this issue](https://github.com/socketio/socket.io-sticky/issues/3).\n\n## License\n\nMIT\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-sticky","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketio%2Fsocket.io-sticky","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-sticky/lists"}