i am running a timer whenever user disconnect then upon reconnection i check if its in video call map then reconnect otherwise call will disconnect the issue is it would work 2-3 times with same data but after that no event is going to that socket id which was disconnected and here is the code of disconnect and reconnect code
scenario- lets say 2 users are on a video call and one user internet connection is gone that means he is disconnect but suddenly his internet comes back so rejoin event will be hit from him but when i emit lets say ping event for testing it will also not go to him but his event would come
socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
totalOnlineUsers--;
io.emit("online-users", totalOnlineUsers);
const userId = socket.userId;
const room = userRooms[socket.id];
if (room) {
reconnectTimers[userId] = setTimeout(async () => {
socket.to(room).emit("peerDisconnected", socket.id);
delete userToSocketMap[userId];
delete userRooms[socket.id];
delete userMinutes[room];
delete userMap[socket.id];
const roomSize = io.sockets.adapter.rooms.get(room)?.size;
if (roomSize === 1 || roomSize === 2) {
const socketIds = Array.from(io.sockets.adapter.rooms.get(room));
socketIds.forEach(async (socketId) => {
const socket = io.sockets.sockets.get(socketId);
socket.leave(room);
delete userRooms[socketId];
delete roomMap[room];
});
activeRooms.delete(room);
io.emit("live-calls", activeRooms.size);
}
console.log("timer")
}, RECONNECT_TIMEOUT);
}
}); socket.on("rejoin", async (data) => {
console.log("rejoinnnnnnnnnnnnnnn", data.userId)
console.log(userToSocketMap)
const userId = data.userId;
console.log("rejoinnnnnnnnnnnnnnn", userToSocketMap[userId])
if (userToSocketMap[userId]) {
console.log("llll")
const previousSocketId = userToSocketMap[userId];
if (reconnectTimers[userId]) {
clearTimeout(reconnectTimers[userId]);
delete reconnectTimers[userId];
}
userRooms[data.socketId] = userRooms[previousSocketId];
socket.join(userRooms[previousSocketId]);
delete userRooms[previousSocketId];
userToSocketMap[userId] = data.socketId;
const room = data.roomId;
const roomSize = io.sockets.adapter.rooms.get(room)?.size;
console.log(roomSize, "roomiee")
if (roomSize === 2) {
console.log("room size 2")
const peerUser = await User.findById(data.userId);
io.to(room).emit("ready", {
roomId: room,
userIds: roomMap[room],
peerIsNew: peerUser.isNewUser,
peerImage: peerUser.userImage,
peerName: peerUser.firstName,
});
}
console.log(data.roomId, "roomId")
io.to(data.socketId).emit("ping", { hellow: "hejejhhje" })
io.to(data.socketId).emit("room", { roomId: data.roomId, userIds: [data.userId] });
return;
}
})
belal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.