John Anderson John Anderson - 3 months ago 13
Node.js Question

Is starting a socket.io server inside a get request ok to do?

New to socket.io and wanted to know if starting a socket server inside of a get request is ok to do if I want to match a session id (http request) with a socket id?

app.get('/', function(req, res, next) {

res.render('index.ejs')


io.on('connection', function (socket) {
console.log(socket)
});


});

Answer

That will attach a additional listener for every request that is made, so it is probably not such a good idea. All previous listeners for io.on('connection', ...) will be executed when the next socket.io connection is made.

Comments