Trying to figure out a sure fire way to confirm all socket methods complete successfully.
In this case I am trying to add an array of rooms (socket.join(roomname)); to a socket and then confirm that they have all been added but I cannot seem to figure out a way to fire console.log(socket.rooms); after it has been set. It always prints the default room and I only see the other rooms after I redundantly emit from the client again.
I am really trying to avoid the usage of a setTimeout or a check with a setInterval and just trying to see how I can make a promise to ensure the socket is joined with each room. Currently the console log only logs the entire socket itself. Anyone have any experience with this?
io.on("connection", socket => {
console.log("New client connected");
socket.on('fetchConvos', async function(rooms) {
let roomsString;
let result = await mapper(rooms); // makes into array
let joinRooms = new Promise((resolve, reject) => { // Promise to add client to all rooms
for (let i = 0; i < result.length; i++) {
resolve(socket.join(result[i]));
}
throw new Error();
})
// Complete putting user in an array of rooms and then print confirmation that rooms were added
joinRooms.then(() => {
console.log(socket.rooms);
result.forEach((string, index) => {
if (string) {
roomsString += string + ", ";
}
});
socket.emit("chat", "You are now in these rooms: " + roomsString); // emit back to socket
}).catch(error => console.log(error));
})
socket.on("disconnect", () => {
console.log("Client disconnected");
});
});