I'm having this simple socket.io manager, where when a user connects, he will be added to a pool of online clients and when another client (say client B), sends a message to client A, It should try to send that message only if client is in the pool. Currently I have this:
var connectionMap = [];
const socketHandler = (io) => {
io.on("connection", (socket) => {
socket.id = "some_custom_id I define";
connectionMap.push(socket);
socket.on("textMessage", function (message) {
connectionMap.forEach(function (connection, index) {
if (connection.id == message.receiver_id)
connection.emit("textMessage", message);
});
});
socket.on("disconnect", function () {
connectionMap.splice(connectionMap.indexOf(client), 1);
});
});
};
Although this might work on a use case with small number of users, I'm looking for a solution that could scale decently without throttling the performance much. (I can't imagine a foreach running across thousands of indexes on every message sent accross the platform.) Any ideas on how to approach this?