I'm trying to make a chat app (similar to Omegle.com) that matches native speakers to learners. I believe I have the correct algorithm, but my Javascript keeps throwing a message that Javascript heap out of memory
error.
For example, let's say Speaker 1
speaks English
and learns French
and Speaker 2
speaks French
and learns English
(perfect match). My algorithm displays match found
.
Queue Controller
socket.emit('in queue', {
speakingLanguages: speakingLanguages,
learningLanguages: learningLanguages
});
socket.on('chat start', function(data){
room = data.room;
$location.path('/chat');
});
server.js
var queue = [];
var rooms = {};
io.sockets.on('connection', function(socket){
console.log('User ' + socket.id + ' connected');
socket.on('in queue', function(data){
socket.speakingLanguages = data.speakingLanguages;
socket.learningLanguages = data.learningLanguages;
if (queue.length != 0){
for (var i = 0; i < queue.length; i++){
for (var j = 0; j < queue[i].speakingLanguages.length; j++){
for (var y = 0; y < socket.learningLanguages.length; y++){
if (queue[i].speakingLanguages[j] === socket.learningLanguages[y]){
console.log('a match was found!');
break;
}else{
queue.push(socket);
break;
}
}
}
}
}else{
queue.push(socket);
}
});
socket.on('send message', function(data){
var room = rooms[socket.id];
io.sockets.in(room).emit('send message', data);
});
});
But see, when I open up multiple windows on my browser to test a lot of cases, I get that error above. It says that I ran out of Javascript heap
and I've been struggling with this for hours, but I don't know what to do.
Information:
speakingLanguages
andlearningLanguages
are of type array (because people can speak multiple languages and learn multiple languages)- Every time a match is not found, I put them into the
queue
array