0

I want to find the socket instance using the socket id

I saw a solution on stackoverflow and tried it, but it didn't work. Make specific socket leave the room is in

exports.removeParticipant = ({ data }) => {
    const { roomId, toBeRemovedSocketId } = data;
    const roomFound = rooms.find((room) => room.id === roomId);
    if (roomFound) {
      const user = roomFound.connectedUsers.find(
        (user) => user.socketId === toBeRemovedSocketId
      );
      /* Removing the user from the room.connectedUserArray . */
      roomFound.connectedUsers = roomFound.connectedUsers.filter(
        (user) => user.socketId !== toBeRemovedSocketId
      );
      //i tried this but it throws error 
      let socket = io.sockets.connected[toBeRemovedSocketId];
      socket.leave(roomId);
    }
   
tedGuy
  • 171
  • 1
  • 7
  • https://stackoverflow.com/questions/38511976/how-can-i-export-socket-io-into-other-modules-in-nodejs/68167653#68167653 – Prathamesh More Sep 02 '22 at 15:08
  • @Prathamesh More thanks for your response but I'm not trying to get the whole io object but the socket object for specific socket id – tedGuy Sep 02 '22 at 15:13

1 Answers1

0

my own research led me to a solution.

let socket=io.sockets.sockets.get(toBeRemovedSocketId);
socket.leave(roomId)
tedGuy
  • 171
  • 1
  • 7