{"id":14978145,"url":"https://github.com/socketio/socket.io-mongo-adapter","last_synced_at":"2025-10-19T11:30:42.836Z","repository":{"id":41864433,"uuid":"372730426","full_name":"socketio/socket.io-mongo-adapter","owner":"socketio","description":"The Socket.IO MongoDB adapter, allowing to broadcast events between several Socket.IO servers","archived":false,"fork":false,"pushed_at":"2024-03-14T17:33:50.000Z","size":494,"stargazers_count":24,"open_issues_count":3,"forks_count":7,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-09-29T01:02:57.290Z","etag":null,"topics":["mongodb","socket-io","websocket"],"latest_commit_sha":null,"homepage":"https://socket.io","language":"TypeScript","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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-01T06:52:35.000Z","updated_at":"2024-06-26T09:19:34.000Z","dependencies_parsed_at":"2024-06-18T16:44:36.913Z","dependency_job_id":"ee8979ce-274c-49f1-b8ce-837205415713","html_url":"https://github.com/socketio/socket.io-mongo-adapter","commit_stats":{"total_commits":37,"total_committers":3,"mean_commits":"12.333333333333334","dds":0.05405405405405406,"last_synced_commit":"ace92ce15d9a7ccfeeffa6ae4293dc98dc155be1"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-adapter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-adapter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-adapter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-adapter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketio","download_url":"https://codeload.github.com/socketio/socket.io-mongo-adapter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219869249,"owners_count":16555572,"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":["mongodb","socket-io","websocket"],"created_at":"2024-09-24T13:56:56.178Z","updated_at":"2025-10-19T11:30:42.357Z","avatar_url":"https://github.com/socketio.png","language":"TypeScript","readme":"# Socket.IO MongoDB adapter\n\nThe `@socket.io/mongo-adapter` package allows broadcasting packets between multiple Socket.IO servers.\n\n\u003cpicture\u003e\n  \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"./assets/adapter_dark.png\"\u003e\n  \u003cimg alt=\"Diagram of Socket.IO packets forwarded through MongoDB\" src=\"./assets/adapter.png\"\u003e\n\u003c/picture\u003e\n\nUnlike the existing [`socket.io-adapter-mongo`](https://github.com/lklepner/socket.io-adapter-mongo) package which uses [tailable cursors](https://docs.mongodb.com/manual/core/tailable-cursors/), this package relies on [change streams](https://docs.mongodb.com/manual/changeStreams/) and thus requires a replica set or a sharded cluster.\n\n**Table of contents**\n\n- [Supported features](#supported-features)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [Usage with a capped collection](#usage-with-a-capped-collection)\n  - [Usage with a TTL index](#usage-with-a-ttl-index)\n- [Known errors](#known-errors)\n- [License](#license)\n\n## Supported features\n\n| Feature                         | `socket.io` version | Support                                        |\n|---------------------------------|---------------------|------------------------------------------------|\n| Socket management               | `4.0.0`             | :white_check_mark: YES (since version `0.1.0`) |\n| Inter-server communication      | `4.1.0`             | :white_check_mark: YES (since version `0.1.0`) |\n| Broadcast with acknowledgements | `4.5.0`             | :white_check_mark: YES (since version `0.2.0`) |\n| Connection state recovery       | `4.6.0`             | :white_check_mark: YES (since version `0.3.0`) |\n\n## Installation\n\n```\nnpm install @socket.io/mongo-adapter mongodb\n```\n\n## Usage\n\nBroadcasting packets within a Socket.IO cluster is achieved by creating MongoDB documents and using a [change stream](https://docs.mongodb.com/manual/changeStreams/) on each Socket.IO server.\n\nThere are two ways to clean up the documents in MongoDB:\n\n- a [capped collection](https://www.mongodb.com/docs/manual/core/capped-collections/)\n- a [TTL index](https://www.mongodb.com/docs/manual/core/index-ttl/)\n\n### Usage with a capped collection\n\n```js\nimport { Server } from \"socket.io\";\nimport { createAdapter } from \"@socket.io/mongo-adapter\";\nimport { MongoClient } from \"mongodb\";\n\nconst DB = \"mydb\";\nconst COLLECTION = \"socket.io-adapter-events\";\n\nconst io = new Server();\n\nconst mongoClient = new MongoClient(\"mongodb://localhost:27017/?replicaSet=rs0\");\n\nawait mongoClient.connect();\n\ntry {\n  await mongoClient.db(DB).createCollection(COLLECTION, {\n    capped: true,\n    size: 1e6\n  });\n} catch (e) {\n  // collection already exists\n}\nconst mongoCollection = mongoClient.db(DB).collection(COLLECTION);\n\nio.adapter(createAdapter(mongoCollection));\nio.listen(3000);\n```\n\n### Usage with a TTL index\n\n```js\nimport { Server } from \"socket.io\";\nimport { createAdapter } from \"@socket.io/mongo-adapter\";\nimport { MongoClient } from \"mongodb\";\n\nconst DB = \"mydb\";\nconst COLLECTION = \"socket.io-adapter-events\";\n\nconst io = new Server();\n\nconst mongoClient = new MongoClient(\"mongodb://localhost:27017/?replicaSet=rs0\");\n\nawait mongoClient.connect();\n\nconst mongoCollection = mongoClient.db(DB).collection(COLLECTION);\n\nawait mongoCollection.createIndex(\n  { createdAt: 1 },\n  { expireAfterSeconds: 3600, background: true }\n);\n\nio.adapter(createAdapter(mongoCollection, {\n  addCreatedAtField: true\n}));\n\nio.listen(3000);\n```\n\n## Known errors\n\n- `MongoError: The $changeStream stage is only supported on replica sets`\n\nChange streams are only available for replica sets and sharded clusters.\n\nMore information [here](https://docs.mongodb.com/manual/changeStreams/).\n\nPlease note that, for development purposes, you can have a single MongoDB process acting as a replica set by running `rs.initiate()` on the node.\n\n- `TypeError: this.mongoCollection.insertOne is not a function`\n\nYou probably passed a MongoDB client instead of a MongoDB collection to the `createAdapter` method.\n\n## License\n\n[MIT](LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-mongo-adapter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketio%2Fsocket.io-mongo-adapter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-mongo-adapter/lists"}