As I delved into the realm of creating a unique Omegle clone using Node.js and Socket.io for educational purposes, I encountered a challenge that has left me scratching my head.
The socket ID of clients along with their interests are stored in an array of objects. To filter out other clients with similar interests, I utilized Lodash. However, I hit a roadblock. In scenarios where no clients share common interests, the search should continue until a match is found. This led me to implement a recursive function with a callback mechanism that triggers the callback upon finding a match, otherwise recursively calls itself.
Unfortunately, this approach resulted in a "maximum call stack exceeded" error. The problematic function is outlined below:
socketApi.funcy = function(socket_id, client_interests, callback){
console.log("I am searching");
search = _.filter(socketApi.available,{interests:client_interests});
_.remove({socketID:socket_id});
if(search.length > 0){
callback();
} else {
socketApi.funcy(socket_id, client_interests, callback);
}
};
Furthermore, here's the complete code snippet for reference:
var socket_io = require('socket.io');
var _ = require('lodash');
var io = socket_io(3001);
var socketApi = {};
socketApi.rooms = [];
socketApi.available = [];
socketApi.taken = [];
socketApi.io = io;
// Rest of the code...
module.exports = socketApi;
I'm seeking guidance on the correct methodology to tackle this issue. Any insights would be greatly appreciated. Thank you in advance!