I am making a simple app using socket.io and node.js and I have started to think about access to array from different events. So when somebody is joining to room I am pushing him to an array of all clients. Then I am looking for a specific client in an array to change some of his attributes. Finally when client disconnects I am removing him from array. Is there a chance that after function finds index of a specific client that client disconnects and the wrong client attribute will be changed? Here is piece of code
let array=[];
io.on('connection', function (client) {
client.on('join',function(data){
array.push(client);
});
client.on('message',function(data){
let index= findOtherClientIndexInArray();
if(index>-1){
//If client leaves the array is altered so the index is not pointing at correct client
array[index].attribute++;
}
});
client.on('leave',function(data){
array.splice(array.indexOf(client),1)
});
});