Advanced Node and Express - Communicate by Emitting Your client is listening for 'user count' event

Advanced Node and Express - Communicate by Emitting Your client is listening for 'user count' event
0

#1
var currentUsers = 0;     
 io.on('connection', socket => {
  console.log('A user has connected');      
    currentUsers++;
          io.emit('user count', currentUsers); 
           socket.on('user count', function(data){
  console.log(data);
});       
});


I also tried the glitch from challenge and got all errors. I laso copied the env. variables


https://learn.freecodecamp.org/information-security-and-quality-assurance/advanced-node-and-express/communicate-by-emitting
I also try
ttp.listen(process.env.PORT || 3000, () => {
console.log("Listening on port " + process.env.PORT); });

var currentUsers = 0; 
      //start socket.io code  
      io.on('connection', (socket) => {
             console.log('A user has connected');  
             ++currentUsers;
           
             socket.on('user count', function(data){ 
               io.emit('user count', currentUsers);  
             console.log(data);
            });     
         socket.on('disconnect', (user) => {
        io.emit('disconnect', user);
        console.log(socket.name + ' has left the chat.');
    });

    });

#2

Did you solve this yet? The socket.on… should be in the client.js file, and should NOT include io.emit within. io.emit should within io.on()…


#3

I started from scratch but even worse all error


#4

Did you remember to fill in the .env file?

Another common issue that it could be, is for the testing to work you need to add app.use(cors()). I did this in the fcctesting.js file within the export. This allows them to run tests remotely against your code. For some reason some of the challenges weren’t set up correctly and so they have the cors dependency installed, and required in the file, but they aren’t using it.


#5

I try and I really fil the nev. without the logs have errors.


I dont remeber what was on original, but form me is suspisious
app/auth.js
there is db.collection(‘chatusers’).findOne(
I am no suer if there sould user or socialuser?
Finally I look in developer mode and it was not allowed origin and I compare the files and that from exercise was wrong

 var allowedOrigins = ['https://pricey-hugger.gomix.me', 'http://pricey-hugger.gomix.me', 'https://f.. '
replace by
const allowedOriginsMatcher = /^https?:\/\/([\w-]+\.)*freecodecamp\.org/;

and

 app.use(function (req, res, next) {
    const origin = req.get('origin');
    if(allowedOriginsMatcher.test(origin)) {
      res.setHeader('Access-Control-Allow-Origin', origin);
    }
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
  });