1

I am struggling to, given a 1-to-1 chat, develop an N-to-N text chat that implements WebRTC protocol with JS. How should I manage to enable the connection of another peer after the one between two peers has already been established? When the firt user joins the chat, he waits for another peer to join the chat and ask to establish a connection. But, when they've already done this, how can I do to make them wait for any other further connection establishment?

The code of the 1-to-1 chat in which i'm basing on is here. Regarding to the code, two things may happen:

  • If a certain peer joins the chat and he's the only one, he is declared as the waiter and waits of another peer, the offerer, to ask for a connection establishment.

  • If when a certain peer joins the chat and there's already a peer registered, it is declared as the offerer and sets up the channel.

In order to enable a user to chat with more than one user, I've tried to declare the first peer as the waiter, and the rest of the peers as offerers, so they establish a connection with all the peers in the chat:

drone.on('open', error => {
    if (error) {
        return console.error(error);
    }
    room = drone.subscribe(roomName);
    room.on('open', error => {
        if (error) {
            return console.error(error);
        }
        console.log('Connected to signaling server');
    });
    // We're connected to the room and received an array of 'members'
    // connected to the room (including us). Signaling server is ready.
    room.on('members', members => {            
        if (members.length == 1) {
            const isOfferer = false;
            startWebRTC(isOfferer);
        } else {
            for (var i = 0; i < members.length; i++) {
                const isOfferer = true;
                startWebRTC(isOfferer);
            }
        }

    });
});

However, when a third user joins the chat, he's unable to chat despite of been detected as another users of the room: .

It appears the following error con console:

RTCDataChannel.readyState is not 'open'

I would be really grateful if someone could give me some advices on how to manage this situation.

Thank you

alexdefelipe
  • 169
  • 2
  • 8
  • Did you look up the error message? [for example](https://stackoverflow.com/questions/22470291/rtcdatachannels-readystate-is-not-open) – James Dec 25 '18 at 15:39
  • Thank you for your answer. No, I didn't because I know (I think) what's the problem with that error: there are no peers listening for a connection establishment as the other two had already connected, so the channel created by the 2nd offerer (the third peer) is not opened yet. So the question is not why is that error appearing, but how to make the two first peers to listen for new connections. – alexdefelipe Dec 25 '18 at 15:46
  • Understood. I was thinking you might spot something useful in the examples/comments. – James Dec 25 '18 at 16:01

0 Answers0