Skip to content

Commit

Permalink
test: add test with mongodb@4
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
darrachequesne committed Jan 19, 2023
1 parent bbe0095 commit ea20cce
Show file tree
Hide file tree
Showing 5 changed files with 4,531 additions and 2,142 deletions.
77 changes: 33 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ Related packages:
npm install @socket.io/mongo-adapter mongodb
```

For TypeScript users, you might also need `@types/mongodb`.

## Usage

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.

There are two ways to clean up the documents in MongoDB:

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

```js
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/mongo-adapter");
const { MongoClient } = require("mongodb");
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/mongo-adapter";
import { MongoClient } from "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);
const mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0");

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);

main();
io.adapter(createAdapter(mongoCollection));
io.listen(3000);
```

### Usage with a TTL index

```js
const { Server } = require("socket.io");
const { createAdapter } = require("@socket.io/mongo-adapter");
const { MongoClient } = require("mongodb");
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/mongo-adapter";
import { MongoClient } from "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 mongoClient = new MongoClient("mongodb://localhost:27017/?replicaSet=rs0");

const main = async () => {
await mongoClient.connect();
await mongoClient.connect();

const mongoCollection = mongoClient.db(DB).collection(COLLECTION);
const mongoCollection = mongoClient.db(DB).collection(COLLECTION);

await mongoCollection.createIndex(
{ createdAt: 1 },
{ expireAfterSeconds: 3600, background: true }
);
await mongoCollection.createIndex(
{ createdAt: 1 },
{ expireAfterSeconds: 3600, background: true }
);

io.adapter(createAdapter(mongoCollection, {
addCreatedAtField: true
}));
io.listen(3000);
}
io.adapter(createAdapter(mongoCollection, {
addCreatedAtField: true
}));

main();
io.listen(3000);
```

## Known errors
Expand Down
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export interface MongoAdapterOptions {
* It seems the `promoteBuffers` option is not always honored, so we manually replace Binary objects by the underlying
* Buffer objects.
*
* Update: it seems to be fixed with `mongodb@4`, but we'll keep it for backward compatibility
*
* Reference:
* - http://mongodb.github.io/node-mongodb-native/3.6/api/Binary.html
* - https://jira.mongodb.org/browse/NODE-1421
Expand Down Expand Up @@ -468,6 +470,8 @@ export class MongoAdapter extends Adapter {
}

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

0 comments on commit ea20cce

Please sign in to comment.