I want to write a real-time chat application with socket.io and because my server and app file are separate, I kind of have no clue that how should I structure it to use socket.io instance in other parts of my app.
This is my app.js
file.
const express = require("express");
const pug = require("pug");
const app = express();
module.exports = app;
and this is my server.js
file
const app = require("./app");
const mongoose = require("mongoose");
mongoose
.connect(`mongodb://localhost:27017/${process.env.DATABASE}`)
.then(() => {
app.listen(process.env.PORT);
})
.catch((err) => {
console.error(`connection failed: ${err}`);
});
what is the best way to create an instance of socket.io and start it's connection for use in other parts of app?