-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 82c3c89
Showing
14 changed files
with
4,700 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
pull_request: | ||
schedule: | ||
- cron: '0 0 * * 0' | ||
|
||
jobs: | ||
test-node: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
matrix: | ||
node-version: [10.x, 12.x, 14.x] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Use Node.js ${{ matrix.node-version }} | ||
uses: actions/setup-node@v1 | ||
with: | ||
node-version: ${{ matrix.node-version }} | ||
|
||
- name: Start MongoDB | ||
uses: supercharge/mongodb-github-action@1.6.0 | ||
with: | ||
mongodb-replica-set: rs0 | ||
|
||
- run: npm ci | ||
|
||
- run: npm test | ||
env: | ||
CI: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
lib-cov | ||
*.seed | ||
*.log | ||
*.csv | ||
*.dat | ||
*.out | ||
*.pid | ||
*.gz | ||
|
||
pids | ||
logs | ||
results | ||
|
||
npm-debug.log | ||
node_modules | ||
.idea | ||
.nyc_output/ | ||
dist/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright (c) 2021 Damien Arrachequesne (@darrachequesne) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# Socket.IO MongoDB adapter | ||
|
||
The `@socket.io/mongo-adapter` package allows broadcasting packets between multiple Socket.IO servers. | ||
|
||
![Adapter diagram](./assets/adapter.png) | ||
|
||
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. | ||
|
||
Supported features: | ||
|
||
- [broadcasting](https://socket.io/docs/v4/broadcasting-events/) | ||
- [utility methods](https://socket.io/docs/v4/server-instance/#Utility-methods) | ||
- [`socketsJoin`](https://socket.io/docs/v4/server-instance/#socketsJoin) | ||
- [`socketsLeave`](https://socket.io/docs/v4/server-instance/#socketsLeave) | ||
- [`disconnectSockets`](https://socket.io/docs/v4/server-instance/#disconnectSockets) | ||
- [`fetchSockets`](https://socket.io/docs/v4/server-instance/#fetchSockets) | ||
- [`serverSideEmit`](https://socket.io/docs/v4/server-instance/#serverSideEmit) | ||
|
||
**Table of contents** | ||
|
||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Known errors](#known-errors) | ||
- [License](#license) | ||
|
||
## Installation | ||
|
||
``` | ||
npm install @socket.io/mongo-adapter mongodb | ||
``` | ||
|
||
For TypeScript users, you might also need `@types/mongodb`. | ||
|
||
## Usage | ||
|
||
```js | ||
const { Server } = require("socket.io"); | ||
const { createAdapter } = require("@socket.io/mongo-adapter"); | ||
const { MongoClient } = require("mongodb"); | ||
|
||
const DB = "mydb"; | ||
const COLLECTION = "socket.io-adapter-events"; | ||
|
||
const io = new Server(); | ||
|
||
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0", { | ||
useUnifiedTopology: true, | ||
}); | ||
|
||
const main = async () => { | ||
await mongoClient.connect(); | ||
|
||
try { | ||
await mongoClient.db(DB).createCollection(COLLECTION, { | ||
capped: true, | ||
size: 1e6 | ||
}); | ||
} catch (e) { | ||
// collection already exists | ||
} | ||
const mongoCollection = mongoClient.db(DB).collection(COLLECTION); | ||
|
||
io.adapter(createAdapter(mongoCollection)); | ||
io.listen(3000); | ||
} | ||
|
||
main(); | ||
``` | ||
|
||
Note: the [capped collection](https://docs.mongodb.com/manual/core/capped-collections/) prevents the collection from growing too big. | ||
|
||
## Known errors | ||
|
||
- `MongoError: The $changeStream stage is only supported on replica sets` | ||
|
||
Change streams are only available for replica sets and sharded clusters. | ||
|
||
More information [here](https://docs.mongodb.com/manual/changeStreams/). | ||
|
||
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. | ||
|
||
- `TypeError: this.mongoCollection.insertOne is not a function` | ||
|
||
You probably passed a MongoDB client instead of a MongoDB collection to the `createAdapter` method. | ||
|
||
## License | ||
|
||
[MIT](LICENSE) |
Oops, something went wrong.