|
| 1 | +# Socket.IO MongoDB adapter |
| 2 | + |
| 3 | +The `@socket.io/mongo-adapter` package allows broadcasting packets between multiple Socket.IO servers. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +Unlike 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. |
| 8 | + |
| 9 | +Supported features: |
| 10 | + |
| 11 | +- [broadcasting](https://socket.io/docs/v4/broadcasting-events/) |
| 12 | +- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods) |
| 13 | + - [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin) |
| 14 | + - [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave) |
| 15 | + - [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets) |
| 16 | + - [`fetchSockets`](https://socket.io/docs/v4/server-instance/#fetchSockets) |
| 17 | + - [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit) |
| 18 | + |
| 19 | +**Table of contents** |
| 20 | + |
| 21 | +- [Installation](#installation) |
| 22 | +- [Usage](#usage) |
| 23 | +- [Known errors](#known-errors) |
| 24 | +- [License](#license) |
| 25 | + |
| 26 | +## Installation |
| 27 | + |
| 28 | +``` |
| 29 | +npm install @socket.io/mongo-adapter mongodb |
| 30 | +``` |
| 31 | + |
| 32 | +For TypeScript users, you might also need `@types/mongodb`. |
| 33 | + |
| 34 | +## Usage |
| 35 | + |
| 36 | +```js |
| 37 | +const { Server } = require("socket.io"); |
| 38 | +const { createAdapter } = require("@socket.io/mongo-adapter"); |
| 39 | +const { MongoClient } = require("mongodb"); |
| 40 | + |
| 41 | +const DB = "mydb"; |
| 42 | +const COLLECTION = "socket.io-adapter-events"; |
| 43 | + |
| 44 | +const io = new Server(); |
| 45 | + |
| 46 | +const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0", { |
| 47 | + useUnifiedTopology: true, |
| 48 | +}); |
| 49 | + |
| 50 | +const main = async () => { |
| 51 | + await mongoClient.connect(); |
| 52 | + |
| 53 | + try { |
| 54 | + await mongoClient.db(DB).createCollection(COLLECTION, { |
| 55 | + capped: true, |
| 56 | + size: 1e6 |
| 57 | + }); |
| 58 | + } catch (e) { |
| 59 | + // collection already exists |
| 60 | + } |
| 61 | + const mongoCollection = mongoClient.db(DB).collection(COLLECTION); |
| 62 | + |
| 63 | + io.adapter(createAdapter(mongoCollection)); |
| 64 | + io.listen(3000); |
| 65 | +} |
| 66 | + |
| 67 | +main(); |
| 68 | +``` |
| 69 | + |
| 70 | +Note: the [capped collection](https://docs.mongodb.com/manual/core/capped-collections/) prevents the collection from growing too big. |
| 71 | + |
| 72 | +## Known errors |
| 73 | + |
| 74 | +- `MongoError: The $changeStream stage is only supported on replica sets` |
| 75 | + |
| 76 | +Change streams are only available for replica sets and sharded clusters. |
| 77 | + |
| 78 | +More information [here](https://docs.mongodb.com/manual/changeStreams/). |
| 79 | + |
| 80 | +Please note that, for development purposes, you can have a single MongoDB process acting as a replica set by running `rs.initiate()` on the node. |
| 81 | + |
| 82 | +- `TypeError: this.mongoCollection.insertOne is not a function` |
| 83 | + |
| 84 | +You probably passed a MongoDB client instead of a MongoDB collection to the `createAdapter` method. |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +[MIT](LICENSE) |
0 commit comments