{"id":14978141,"url":"https://github.com/socketio/socket.io-mongo-emitter","last_synced_at":"2025-10-19T11:30:40.817Z","repository":{"id":57161292,"uuid":"372753640","full_name":"socketio/socket.io-mongo-emitter","owner":"socketio","description":"The Socket.IO MongoDB emitter, allowing to communicate with a group of Socket.IO servers from another Node.js process","archived":false,"fork":false,"pushed_at":"2023-06-27T15:03:14.000Z","size":181,"stargazers_count":7,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-01-29T13:11:23.622Z","etag":null,"topics":["mongodb","socket-io","websocket"],"latest_commit_sha":null,"homepage":"","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}},"created_at":"2021-06-01T08:18:26.000Z","updated_at":"2025-01-08T23:43:47.000Z","dependencies_parsed_at":"2024-01-23T21:29:53.604Z","dependency_job_id":"ac1eafc8-07b2-4dfd-a992-970b9de759f6","html_url":"https://github.com/socketio/socket.io-mongo-emitter","commit_stats":{"total_commits":3,"total_committers":1,"mean_commits":3.0,"dds":0.0,"last_synced_commit":"2e33ccec9e2d5d04c8444328541f341314112034"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-emitter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-emitter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-emitter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketio%2Fsocket.io-mongo-emitter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketio","download_url":"https://codeload.github.com/socketio/socket.io-mongo-emitter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237116630,"owners_count":19258285,"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:55.825Z","updated_at":"2025-10-19T11:30:40.403Z","avatar_url":"https://github.com/socketio.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Socket.IO MongoDB emitter\n\nThe `@socket.io/mongo-emitter` package allows you to easily communicate with a group of Socket.IO servers from another Node.js process (server-side).\n\n![Emitter diagram](./assets/emitter.png)\n\nIt must be used in conjunction with [`@socket.io/mongo-adapter`](https://github.com/socketio/socket.io-mongo-adapter/).\n\nSupported features:\n\n- [broadcasting](https://socket.io/docs/v4/broadcasting-events/)\n- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods)\n  - [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin)\n  - [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave)\n  - [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets)\n  - [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit)\n\nRelated packages:\n\n- MongoDB adapter: https://github.com/socketio/socket.io-mongo-adapter/\n- Redis adapter: https://github.com/socketio/socket.io-redis-adapter/\n- Redis emitter: https://github.com/socketio/socket.io-redis-emitter/\n- Postgres adapter: https://github.com/socketio/socket.io-postgres-adapter/\n- Postgres emitter: https://github.com/socketio/socket.io-postgres-emitter/\n\n**Table of contents**\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n- [Known errors](#known-errors)\n- [License](#license)\n\n## Installation\n\n```\nnpm install @socket.io/mongo-emitter mongodb\n```\n\nFor TypeScript users, you might also need `@types/mongodb`.\n\n## Usage\n\n```js\nconst { Emitter } = require(\"@socket.io/mongo-emitter\");\nconst { MongoClient } = require(\"mongodb\");\n\nconst DB = \"mydb\";\nconst COLLECTION = \"socket.io-adapter-events\";\n\nconst mongoClient = new MongoClient(\"mongodb://localhost:27017/?replicaSet=rs0\", {\n  useUnifiedTopology: true,\n});\n\nconst main = async () =\u003e {\n  await mongoClient.connect();\n\n  const mongoCollection = mongoClient.db(DB).collection(COLLECTION);\n  const io = new Emitter(mongoCollection);\n\n  setInterval(() =\u003e {\n    io.emit(\"ping\", new Date());\n  }, 1000);\n}\n\nmain();\n```\n\n## API\n\n### `Emitter(mongoCollection[, nsp])`\n\n```js\nconst io = new Emitter(mongoCollection);\n```\n\nThe `mongoCollection` argument is a MongoDB [collection object](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html) from the `mongodb` package.\n\n### `Emitter#to(room:string):BroadcastOperator`\n### `Emitter#in(room:string):BroadcastOperator`\n\nSpecifies a specific `room` that you want to emit to.\n\n```js\nio.to(\"room1\").emit(\"hello\");\n```\n\n### `Emitter#except(room:string):BroadcastOperator`\n\nSpecifies a specific `room` that you want to exclude from broadcasting.\n\n```js\nio.except(\"room2\").emit(\"hello\");\n```\n\n### `Emitter#of(namespace:string):Emitter`\n\nSpecifies a specific namespace that you want to emit to.\n\n```js\nconst customNamespace = io.of(\"/custom\");\n\ncustomNamespace.emit(\"hello\");\n```\n\n### `Emitter#socketsJoin(rooms:string|string[])`\n\nMakes the matching socket instances join the specified rooms:\n\n```js\n// make all Socket instances join the \"room1\" room\nio.socketsJoin(\"room1\");\n\n// make all Socket instances of the \"admin\" namespace in the \"room1\" room join the \"room2\" room\nio.of(\"/admin\").in(\"room1\").socketsJoin(\"room2\");\n```\n\n### `Emitter#socketsLeave(rooms:string|string[])`\n\nMakes the matching socket instances leave the specified rooms:\n\n```js\n// make all Socket instances leave the \"room1\" room\nio.socketsLeave(\"room1\");\n\n// make all Socket instances of the \"admin\" namespace in the \"room1\" room leave the \"room2\" room\nio.of(\"/admin\").in(\"room1\").socketsLeave(\"room2\");\n```\n\n### `Emitter#disconnectSockets(close:boolean)`\n\nMakes the matching socket instances disconnect:\n\n```js\n// make all Socket instances disconnect\nio.disconnectSockets();\n\n// make all Socket instances of the \"admin\" namespace in the \"room1\" room disconnect\nio.of(\"/admin\").in(\"room1\").disconnectSockets();\n\n// this also works with a single socket ID\nio.of(\"/admin\").in(theSocketId).disconnectSockets();\n```\n\n### `Emitter#serverSideEmit(ev:string[,...args:any[]])`\n\nEmits an event that will be received by each Socket.IO server of the cluster.\n\n```js\nio.serverSideEmit(\"ping\");\n```\n\n## Known errors\n\n- `TypeError: this.mongoCollection.insertOne is not a function`\n\nYou probably passed a MongoDB client instead of a MongoDB collection to the `Emitter` constructor.\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-mongo-emitter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketio%2Fsocket.io-mongo-emitter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketio%2Fsocket.io-mongo-emitter/lists"}