'Party calls with peerjs

I'm trying to make a voice chat using some particular voice channels. I'm having trouble getting more than two users to interact at a time though.

From what I understand when it creates a call it seems that somehow it interrupts the previous one.

I've tried following Multi Party - Video Conference with peerJS

and convert it to make it compatible with my

Would anyone know the solution?

// when the user connect to the channel
  socket.on('user-connected', (data) => {
    if(document.querySelector(`#channel-user-${data.user_id}`) !== null) return;
    const connectedUsers = document.querySelectorAll(`#channel-image-${data.channel_id}`)[0];

    if (data.user_id === user_id && connectedUsers !== undefined) {
      for (let i = 0; i < connectedUsers.children.length; i++) { 

        // get all the users inside the channel
        const element = connectedUsers.children[i];
        const otherUser = element.id.split('-')[element.id.split('-').length - 1] 
        //this is the peer id

        // call it
        const call = peer.call(otherUser, localStream);
        call.on('stream', stream => {
          remoteStream = stream;
          const audio = document.createElement('audio');
          audio.srcObject = stream;
          audio.play();
        })
      }
    }
  //when receiving a call answer and play it
  peer.on('call', call => {
    console.log('Received call by', call.peer);
    call.answer(localStream);
    call.on('stream', stream => {
      remoteStream = stream;
      const audio = document.createElement('audio');
      audio.srcObject = stream;
      audio.play();
    })
  });


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source