node.js – My approach on nextjs clustering and socket handeling

I’m interested in feedback on my approach to handling cluster on Next.js SSG app with express hosted on Heroku.

The app is working however, please let me know if this is the wrong approach or if you see any potential mistake.

Project specs:
sticky-session : “^1.1.2”
express : “^4.17.1”,
next : “^10.0.5”,
socket.io : “^2.4.0”,
socket.io-client : “^2.4.0”,
socket.io-redis : “^5.4.2”

Server code:
const sticky = require(“sticky-session”);

const app = require("express")();
const server = require("http").Server(app);
const io = require("socket.io").listen(server);
io.set("transports", ("websocket"));
const redis = require("socket.io-redis");

const PORT = process.env.PORT || 3000;
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

const keys = require("./config/keys");
const mongoose = require("mongoose");
const cookieSession = require("cookie-session");
const passport = require("passport");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const sslRedirect = require("heroku-ssl-redirect").default;

io.adapter(redis(keys.REDIS_URL));

const worker = () => {
    nextApp
        .prepare()
        .then(() => {
            app.use(cookieParser());

            app.use(bodyParser.json());

            app.use(
                cookieSession({
                    maxAge: 30 * 24 * 60 * 60 * 1000,
                    keys: (keys.cookieKey),
                })
            );

            app.use(passport.initialize());
            app.use(passport.session()); // persistent login sessions

            // IMPORT MODELS
            mongoose.connect(keys.mongoURI, {
                useNewUrlParser: true,
                useUnifiedTopology: true,
            });

            const db = mongoose.connection;

            db.once("open", function () {
                console.log("MongoDB database connection established successfully");
            });
            db.on("error", console.error.bind(console, "MongoDB connection error:"));



            require("./sockets/index")(io);
            require("./routes/api")(app, io);

            app.get("*", (req, res) => {
                return handle(req, res);
            });
        })
        .catch((ex) => {
            console.error(ex.stack);
            process.exit(1);
        });
};

//sticky.listen() will return false if Master
if (!sticky.listen(server, PORT)) {
} else {
    worker();
}

On the client I simply use:

const socket = io({ transports: ("websocket") });

Heroku settings:

heroku features:enable http-session-affinity