title: Cannot establish peer to peer webrtc connection in node.js
body: <p>I am using a WebRTC npm package named wrtc and the built-in net library of node.js. For some reason I can't establish connection. I get the error:</p>
  <pre class="lang-sh prettyprint-override"><code>C:\dev\WebRTCchat\node_modules\wrtc\lib\index.js:49
    this._send(data);
         ^
  DOMException [InvalidStateError]: RTCDataChannel.readyState is not 'open
  '
      at RTCDataChannel.send (C:\dev\WebRTCchat\node_modules\wrtc\lib\inde
  x.js:49:8)
      at C:\dev\WebRTCchat\clientA.js:35:35
  </code></pre>
  <p>Any Ideas? So here is the client A code (who creates the offer)</p>
  <p>clientA.js</p>
  <pre class="lang-js prettyprint-override"><code>const net = require('net');
  const { RTCPeerConnection } = require(&quot;wrtc&quot;);
  
  const connA = new RTCPeerConnection();
  connA.onicecandidate = () =&gt; console.log(&quot;Ice candidate added. &quot;);// + JSON.stringify(connA.localDescription));
  const dc = connA.createDataChannel(&quot;channel&quot;);
  dc.onmessage = e =&gt; console.log(&quot;Message -&gt; &quot; + e.data);
  dc.onopen = () =&gt; console.log(&quot;Client connected!&quot;);
  
  function delay(ms) {
      return new Promise(res =&gt; setTimeout(res, ms));
  }
  
  function createSDPOffer() {
      return new Promise(resolve =&gt; connA.createOffer()
          .then(o =&gt; {
              connA.setLocalDescription(o);
              console.log(&quot;Local Description set!&quot;);
              resolve(o);
          }));
  }
  
  function getSDPAnswer(answer) {
      connA.setRemoteDescription(answer).then(a =&gt; console.log(&quot;Remote description set!&quot;));
  }
  
  const server = net.createServer();
  server.on('connection', socket =&gt; {
      createSDPOffer().then(offer =&gt; {
          socket.write(JSON.stringify(offer));
      })
  
      socket.on('data', data =&gt; {
          getSDPAnswer(JSON.parse(data));
          delay(1000).then(() =&gt; dc.send(&quot;Hello I am client A&quot;));
      })
  })
  
  server.listen(1337, '127.0.0.1');
  
  </code></pre>
  <p>And then client B (the one who receives the offer and creates the answer)</p>
  <p>clientB.js</p>
  <pre class="lang-js prettyprint-override"><code>const net = require('net');
  const { RTCPeerConnection } = require(&quot;wrtc&quot;);
  
  const connB = new RTCPeerConnection();
  const socket = new net.Socket();
  
  function delay(ms) {
      return new Promise(res =&gt; setTimeout(res, ms));
  }
  
  socket.connect(1337, &quot;127.0.0.1&quot;);
  
  socket.on('data', (data) =&gt; {
      getSDPOffer(JSON.parse(data));
  
      sendSDPAnswer().then(a =&gt; socket.write(JSON.stringify(a)));
  
      delay(2000).then(() =&gt; {
          connB.dc.send(&quot;Hello I am B&quot;)
      });
  
  });
  
  socket.on('close', () =&gt; {
  
  })
  
  
  function getSDPOffer(offer) {
      connB.onicecandidate = () =&gt; console.log(&quot;Ice candidate added. &quot;); //+ JSON.stringify(connB.localDescription));
  
      connB.ondatachannel = e =&gt; {
          connB.dc = e.channel;
          connB.dc.onopen = () =&gt; console.log(&quot;Client connected!&quot;);
          connB.dc.onmessage = e =&gt; console.log(&quot;Message -&gt; &quot; + e.data);
      }
  
      connB.setRemoteDescription(offer)
          .then(() =&gt; console.log(&quot;Remote Description Offer set!&quot;));
  }
  
  function sendSDPAnswer() {
      return new Promise(resolve =&gt; {
          connB.createAnswer()
              .then(a =&gt; {
                  connB.setLocalDescription(a);
                  console.log(&quot;Local Description set!&quot;);
                  resolve(a);
              });
      })
  }
  </code></pre>