I'm currently trying to get a list of all of the members that are signed in. I'm using socket.js for this and using event handlers.
I have the following event:
eventEmitter.on('userSignedIn', function(socket, user) {
socket.emit("userJoined", {currentUsers: user.first_name});
});
Then, in JQuery, I am doing the following:
socket.on('userJoined', function (message) {
newItem = $("<li>Item " + message.currentUsers + "</li>").show();
$('.users-list').prepend(newItem);
});
This however is not working and not even showing the correct username(s).
Basically, what I want to do is when the event userSignedIn it appends an array users with the user that's just signed and then send this through the socket. But I'm struggling to see how I can store this inside the global app?
A global variable in node is accessed through process.env.
If you need to store this as a global variable in Node, go to the dotenv module, install it, (farily simple to use it) and use it to store your original global for the counter. In your .env file you would have say
counter=1. Then you can increment it by accessing the global in your code by stating process.env.counter and you can increment it as well.Remember: if you process stops running or goes down the counter will return to the original state of 1 when you bring it back up.
You can if you wish not use the dotenv module and simply create and access a process.env variable straight in your code. However, dotenv is very flexible, and you may want to use it for other purposes.
Hope this helps