Skip to content

Commit

Permalink
docs: add example with capped collection and TTL index
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 23, 2024
1 parent b80c9bb commit ace92ce
Show file tree
Hide file tree
Showing 6 changed files with 3,819 additions and 0 deletions.
79 changes: 79 additions & 0 deletions examples/capped-example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { MongoClient } from "mongodb";
import { Server } from "socket.io";
import { createAdapter } from "@socket.io/mongo-adapter";
import { io } from "socket.io-client";

async function initMongoCollection() {
const DB = "mydb";
const COLLECTION = "socket.io-adapter-events-capped";

const mongoClient = new MongoClient(
"mongodb://localhost:27017/?replicaSet=rs0&directConnection=true"
);

await mongoClient.connect();

try {
await mongoClient.db(DB).createCollection(COLLECTION, {
capped: true,
size: 1e6,
});
} catch (e) {
// collection already exists
}

return mongoClient.db(DB).collection(COLLECTION);
}

function initServer(mongoCollection) {
const io = new Server({
connectionStateRecovery: {},
});

io.adapter(createAdapter(mongoCollection));

return io;
}

function initClient(port) {
const socket = io(`http://localhost:${port}`);

socket.on("connect", () => {
console.log(`[${port}] connected (recovered? ${socket.recovered})`);
});

socket.on("ping", () => {
console.log(`[${port}] got ping`);
});

socket.on("disconnect", (reason) => {
console.log(`[${port}] disconnected due to ${reason}`);
});

return socket;
}

const mongoCollection = await initMongoCollection();

const io1 = initServer(mongoCollection);
const io2 = initServer(mongoCollection);
const io3 = initServer(mongoCollection);

io1.listen(3000);
io2.listen(3001);
io3.listen(3002);

initClient(3000);
initClient(3001);
initClient(3002);

setInterval(() => {
io1.emit("ping");
}, 2000);

// uncomment to test connection state recovery
// setTimeout(() => {
// io2.close(() => {
// io2.listen(3001);
// });
// }, 3000);
Loading

0 comments on commit ace92ce

Please sign in to comment.