WebRTC Data Channel connection established but messages do not come
<p>I am having trouble receiving WebRTC Data Channels messages between two peers.</p>

<p>Below is an MVP where both peers are one the same page and signalling channel is replaced with plain JavaScript object exchange.</p>

<p>I do not use trickle ICE by choice and I am aware of the drawbacks of not using it, I do not believe it should interfere with my MVP below in any way.</p>

<p>The code behaves exactly as expected and all the logging messages go through. The only ones missing are the ones for data channel message handling, as if the message never came from either peer to the other. That's with the event handler that attempts to send <em>Test</em>. The <em>send</em> method call fails with a null reference error which I couldn't track the root cause of.</p>

<p>I have followed <a href="https://www.pkcsecurity.com/blog" rel="nofollow">Untangling the WebRTC Flow</a> to get where I am with this MVP. <a href="https://tomashubelbauer.gitlab.io/peer-note/experiment2.html" rel="nofollow">You can see it live here so you don't have to run it yourself.</a></p>

<pre><code>class Peer {
    constructor(name) {
        this.name = name;
    }
    offer() {
        const peerConnection = new webkitRTCPeerConnection({ iceServers: [ { url: "stun:stun.l.google.com:19302" } ] });
        peerConnection.onnegotiationneeded = event =&gt; console.debug(this.name, "onnegotiationneeded");
        peerConnection.onsignalingstatechange = event =&gt; console.debug(this.name, "onsignalingstatechange", peerConnection.signalingState);
        peerConnection.onicegatheringstatechange = event =&gt; console.debug(this.name, "onicegatheringstatechange", peerConnection.iceGatheringState);
        peerConnection.oniceconnectionstatechange = event =&gt; console.debug(this.name, "oniceconnectionstatechange", peerConnection.iceConnectionState);
        peerConnection.onconnectionstatechange = event =&gt; console.debug(this.name, "onconnectionstatechange", peerConnection.connectionState);
        peerConnection.ondatachannel = event =&gt; {
        const dataChannel = event.channel;
        dataChannel.onopen = event =&gt; {
            console.debug(this.name, "onopen");
            dataChannel.send("TEST");
        };
        dataChannel.onclose = event =&gt; console.debug(this.name, "onclose");
        dataChannel.onerror = event =&gt; console.debug(this.name, "onerror");
        dataChannel.onmessage = event =&gt; console.debug(this.name, "onmessage");
        console.debug(this.name, "ondatachannelO");
        this.dataChannel = dataChannel;
        };

        return new Promise((resolve, reject) =&gt; {
        peerConnection.onicecandidate = event =&gt; {
            if (!event.candidate) {
            peerConnection.createOffer()
                .then(offer =&gt; {
                console.debug(this.name, "created an offer with candidates.");
                this.peerConnection = peerConnection;
                resolve(peerConnection.localDescription);
                })
                .catch(reject);
            }
        };
        peerConnection.createDataChannel("datachannel");
        peerConnection.createOffer()
            .then(offer =&gt; {
            console.debug(this.name, "created an offer without candidates.");
            peerConnection.setLocalDescription(offer)
                .then(() =&gt; {
                console.debug(this.name, "set local description. Collecting candidates…");
                })
                .catch(reject);
            })
            .catch(reject);
        });
    }
    answer(offer) {
        const peerConnection = new webkitRTCPeerConnection({ iceServers: [ { url: "stun:stun.l.google.com:19302" } ] });
        peerConnection.onnegotiationneeded = event =&gt; console.debug(this.name, "onnegotiationneeded");
        peerConnection.onsignalingstatechange = event =&gt; console.debug(this.name, "onsignalingstatechange", peerConnection.signalingState);
        peerConnection.onicegatheringstatechange = event =&gt; console.debug(this.name, "onicegatheringstatechange", peerConnection.iceGatheringState);
        peerConnection.oniceconnectionstatechange = event =&gt; console.debug(this.name, "oniceconnectionstatechange", peerConnection.iceConnectionState);
        peerConnection.onconnectionstatechange = event =&gt; console.debug(this.name, "onconnectionstatechange", peerConnection.connectionState);
        peerConnection.ondatachannel = event =&gt; {
        const dataChannel = event.channel;
        dataChannel.onopen = event =&gt; {
            console.debug(this.name, "onopen");
            dataChannel.send("TEST");
        };
        dataChannel.onclose = event =&gt; console.debug(this.name, "onclose");
        dataChannel.onerror = event =&gt; console.debug(this.name, "onerror");
        dataChannel.onmessage = event =&gt; console.debug(this.name, "onmessage");
        console.debug(this.name, "ondatachannelA");
        this.dataChannel = dataChannel;
        };
        return new Promise((resolve, reject) =&gt; {
        peerConnection.onicecandidate = event =&gt; {
            if (!event.candidate) {
            peerConnection.createAnswer()
                .then(answer =&gt; {
                console.debug(this.name, "created an answer with candidates.");
                resolve(peerConnection.localDescription);
                })
                .catch(reject);
            }
        };
        peerConnection.setRemoteDescription(offer)
            .then(() =&gt; {
            console.debug(this.name, "set remote description.");
            peerConnection.createAnswer()
                .then(answer =&gt; {
                console.debug(this.name, "created an answer without candidates.");
                peerConnection.setLocalDescription(answer)
                    .then(() =&gt; {
                    console.debug(this.name, "set local description.");
                    })
                    .catch(reject);
                })
                .catch(reject);
            })
            .catch(reject);
        });
    }
    sealTheDeal(proffer) {
        return new Promise((resolve, reject) =&gt; {
        this.peerConnection.setRemoteDescription(proffer)
            .then(() =&gt; {
            console.debug(this.name, "set remote description.");
            resolve();
            })
            .catch(console.e);
        });
    }
    send() {
        this.dataChannel.send("TEST");
    }
    }
    function flow() {
    const peerA = new Peer("Alice");
    const peerB = new Peer("Bob");
    peerA.offer()
        .then(offer =&gt; {
        console.debug("Signal transfering offer from Alice to Bob.");
        peerB.answer(offer)
            .then(proffer =&gt; {
            console.debug("Signal transfering proffer from Bob to Alice.");
            peerA.sealTheDeal(proffer)
                .then(() =&gt; {
                peerB.offer()
                    .then(offer =&gt; {
                    console.debug("Signal transfering offer from Bob to Alice.");
                    peerA.answer(offer)
                        .then(proffer =&gt; {
                        console.debug("Signal transfering proffer from Alice to Bob.");
                        peerB.sealTheDeal(proffer)
                            .then(() =&gt; {
                            console.debug("HYPE");
                            peerA.send("From Alice to Bob.");
                            peerB.send("From Bob to Alice.");
                            })
                            .catch(console.error);
                        })
                        .catch(console.error);
                    })
                    .catch(console.error);
                })
                .catch(console.error);
            })
            .catch(console.error);
        })
        .catch(console.error);
        window.peerA = peerA;
        window.peerB = peerB;
    }
    flow();
</code></pre>