Skip to content

Commit ea20cce

Browse files
test: add test with mongodb@4
- the types are now included in the `mongodb` package - the "promoteBuffers" option now works properly - the "useUnifiedTopology" option was removed Reference: https://github.com/mongodb/node-mongodb-native/blob/main/etc/notes/CHANGES_4.0.0.md Related: - #10 - socketio/socket.io-parser@ae8dd88
1 parent bbe0095 commit ea20cce

File tree

5 files changed

+4531
-2142
lines changed

5 files changed

+4531
-2142
lines changed

README.md

+33-44
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ Related packages:
3737
npm install @socket.io/mongo-adapter mongodb
3838
```
3939

40-
For TypeScript users, you might also need `@types/mongodb`.
41-
4240
## Usage
4341

42+
Broadcasting 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.
43+
4444
There are two ways to clean up the documents in MongoDB:
4545

4646
- a [capped collection](https://www.mongodb.com/docs/manual/core/capped-collections/)
@@ -49,72 +49,61 @@ There are two ways to clean up the documents in MongoDB:
4949
### Usage with a capped collection
5050

5151
```js
52-
const { Server } = require("socket.io");
53-
const { createAdapter } = require("@socket.io/mongo-adapter");
54-
const { MongoClient } = require("mongodb");
52+
import { Server } from "socket.io";
53+
import { createAdapter } from "@socket.io/mongo-adapter";
54+
import { MongoClient } from "mongodb";
5555

5656
const DB = "mydb";
5757
const COLLECTION = "socket.io-adapter-events";
5858

5959
const io = new Server();
6060

61-
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0", {
62-
useUnifiedTopology: true,
63-
});
64-
65-
const main = async () => {
66-
await mongoClient.connect();
67-
68-
try {
69-
await mongoClient.db(DB).createCollection(COLLECTION, {
70-
capped: true,
71-
size: 1e6
72-
});
73-
} catch (e) {
74-
// collection already exists
75-
}
76-
const mongoCollection = mongoClient.db(DB).collection(COLLECTION);
77-
78-
io.adapter(createAdapter(mongoCollection));
79-
io.listen(3000);
61+
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0");
62+
63+
await mongoClient.connect();
64+
65+
try {
66+
await mongoClient.db(DB).createCollection(COLLECTION, {
67+
capped: true,
68+
size: 1e6
69+
});
70+
} catch (e) {
71+
// collection already exists
8072
}
73+
const mongoCollection = mongoClient.db(DB).collection(COLLECTION);
8174

82-
main();
75+
io.adapter(createAdapter(mongoCollection));
76+
io.listen(3000);
8377
```
8478

8579
### Usage with a TTL index
8680

8781
```js
88-
const { Server } = require("socket.io");
89-
const { createAdapter } = require("@socket.io/mongo-adapter");
90-
const { MongoClient } = require("mongodb");
82+
import { Server } from "socket.io";
83+
import { createAdapter } from "@socket.io/mongo-adapter";
84+
import { MongoClient } from "mongodb";
9185

9286
const DB = "mydb";
9387
const COLLECTION = "socket.io-adapter-events";
9488

9589
const io = new Server();
9690

97-
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0", {
98-
useUnifiedTopology: true,
99-
});
91+
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0");
10092

101-
const main = async () => {
102-
await mongoClient.connect();
93+
await mongoClient.connect();
10394

104-
const mongoCollection = mongoClient.db(DB).collection(COLLECTION);
95+
const mongoCollection = mongoClient.db(DB).collection(COLLECTION);
10596

106-
await mongoCollection.createIndex(
107-
{ createdAt: 1 },
108-
{ expireAfterSeconds: 3600, background: true }
109-
);
97+
await mongoCollection.createIndex(
98+
{ createdAt: 1 },
99+
{ expireAfterSeconds: 3600, background: true }
100+
);
110101

111-
io.adapter(createAdapter(mongoCollection, {
112-
addCreatedAtField: true
113-
}));
114-
io.listen(3000);
115-
}
102+
io.adapter(createAdapter(mongoCollection, {
103+
addCreatedAtField: true
104+
}));
116105

117-
main();
106+
io.listen(3000);
118107
```
119108

120109
## Known errors

lib/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ export interface MongoAdapterOptions {
7676
* It seems the `promoteBuffers` option is not always honored, so we manually replace Binary objects by the underlying
7777
* Buffer objects.
7878
*
79+
* Update: it seems to be fixed with `mongodb@4`, but we'll keep it for backward compatibility
80+
*
7981
* Reference:
8082
* - http://mongodb.github.io/node-mongodb-native/3.6/api/Binary.html
8183
* - https://jira.mongodb.org/browse/NODE-1421
@@ -468,6 +470,8 @@ export class MongoAdapter extends Adapter {
468470
}
469471

470472
// packets with binary contents are modified by the broadcast method, hence the nextTick()
473+
// update: this should be fixed now, but we'll keep it for backward compatibility
474+
// see: https://github.com/socketio/socket.io-parser/commit/ae8dd88995dbd7f89c97e5cc15e5b489fa0efece
471475
process.nextTick(() => {
472476
super.broadcast(packet, opts);
473477
});

0 commit comments

Comments
 (0)