1👍
✅
It’s because you’re creating a connection listener every time someone visits the '/'
route. Try moving the socket-io code outside of the '/'
route function.
1👍
As Eric mentioned, move your connection listener outside of the /
route in order to prevent it from getting created every time someone visits the page.
router.get('/', function(req, res, next) {
res.render('index', { title: 'Express' });
});
io.on('connection', function(socket) {
socket.on('chatMessage', function(msg){
io.emit('chatMessage', msg);
});
});
Source:stackexchange.com