Next.js Discord

Discord Forum

Issues hosting with websockets

Unanswered
Spotted Redshank posted this in #help-forum
Open in Discord
Spotted RedshankOP
hi

4 Replies

Spotted RedshankOP
const { createServer } = require("http");
const next = require("next");
const { Server } = require("socket.io");

const dev = process.env.NODE_ENV !== "production";
const hostname = process.env.HOSTNAME || "localhost";
const port = process.env.PORT || 3000;
const app = next({ dev, hostname, port });
// const app = next({ dev });

const handler = app.getRequestHandler();

app.prepare().then(() => {

    const httpServer = createServer(handler);
    const io = new Server(httpServer, {
        cors: {
            origin: "*",
            methods: ["GET", "POST"]
        }
    }); const mousePositions = {};
    const users = {};

    io.on("connection", (socket) => {

       

    });

    httpServer.listen(process.env.PORT, (err) => {
        if (err) {
            console.log(err);
            throw err;
        }
        console.log(`> Ready on ${hostname} ${port}`);
    });
});
NODE_ENV=development
PORT=80
HOSTNAME=0.0.0.0
My websockets do work on local but the host just doesn't seem to want to work