Home > Enterprise >  How to run socket.io after user enter the port number in express?
How to run socket.io after user enter the port number in express?

Time:10-01

I want to run socket.io after user enter number of port in the express if user doesn't enter number start at default port else start at user port number. But i can't find the solution.

I wrote this , but not working.

router.get("/run", (req, res, next) => {
      let IO = socketIo.listen();
    
      IO.on("connection", (client) => {
        console.log("On");
        console.log(client.handshake.query);
      });

      next();

    });

CodePudding user response:

Try this way:

let socketPort = 3000;
const io = require('socket.io')();
io.on('connection', client => { /* your code */ });

router.get("/run", (req,res)=>{
    socketPort = req.query.port || socketPort
    io.listen(Number(socketPort))
})

Hope this helps! feel free to comment if you are stuck with any errors.

  • Related