Created
July 5, 2022 18:41
-
-
Save toluolatubosun/df0de26f3c4133793a550e8ea8fb6143 to your computer and use it in GitHub Desktop.
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
const { createAdapter } = require("@socket.io/mongo-adapter"); | |
const MONGODB_URI = "mongodb://localhost:27017/app"; | |
const params = { | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}; | |
mongoose.connect(MONGODB_URI, params, async (err, data) => { | |
if (err) { | |
console.error("<::: Couldn't connect to database", err); | |
} else { | |
console.log(`:::> Connected to MongoDB database. ${MONGODB_URI}`); | |
// Initialize Socket.io Adapter | |
const mongooseCollection = mongoose.connection.collection("adapter"); | |
await mongooseCollection.createIndex( | |
{ createdAt: 1 }, | |
{ expireAfterSeconds: 3600, background: true } | |
) | |
io.adapter(createAdapter(mongooseCollection, { addCreatedAtField: true })); | |
} | |
}); | |
// Initialization WebSocket | |
io.use((socket, next) => { | |
return require("./middlewares/auth.middleware")(socket, next); | |
}).on("connection", (socket) => { | |
console.log(`:::> User ${socket.$user.username} connected`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment