title: WebRTC - How to establish a peer connection after offers and answers
body: <p>I have a node.js running which the users will connect to. The offer and answer will be generated and sent through node.js.</p>
  
  <p>I'm trying to establish a peer connection and send over a camera stream. I tried my code without using ICE candidates as the computers where in the same subnet. I tried to implement ICE afterwards. I'm not sure if i've done it right though or if it's even needed if the computers are on the same subnet.</p>
  
  <pre><code>var localStream;
  //Connect to signaling server
  var signalingChannel = io.connect('http://85.134.54.193:8001');
  console.log("Connect to signaling server");
  var servers = null;
  
  var video1;
  var video2;
  var audio1;
  var audio2;
  
  var cfg = {"iceServers":[{"url":"stun:stun.l.google.com:19302"}]};//{ "iceServers": [{ "url": "stun:stun.l.google.com:19302" }] };
  var con = { 'optional': [{'DtlsSrtpKeyAgreement': true}, {'RtpDataChannels': true }] };
  var peerConnection;
  
  //Runs after the page has been loaded
  window.onload=function(){
      //Gets ID for the video element which will display the local stream
      video1 = document.getElementById("audio1");
      //Gets ID for the video element which will display the remote stream
      video2 = document.getElementById("audio2");
      audio1 = document.getElementById("audio1");
      audio2 = document.getElementById("audio2");
      }
  
  //Start button function
  function caller(){
      peerConnection = new webkitRTCPeerConnection(cfg);
      navigator.webkitGetUserMedia({'audio':true, video:true}, function (stream) {
          console.log("Got local audio", stream);
          video1.src = window.webkitURL.createObjectURL(stream)
          peerConnection.addStream(stream);
      }, 
      function ( err ) {
          console.log( 'error: ', err );
      });
  
      console.log("Calling");
      //Create Offer
      peerConnection.createOffer(function (offerDesc) {
          console.log("Created local offer", offerDesc.sdp);
          peerConnection.setLocalDescription(offerDesc);
      }, function () { console.warn("Couldn't create offer"); });
  
      //ICE Candidates Generator
      peerConnection.onicecandidate = function(evt) {
          //When The Ice Gathering is complete
          if (evt.target.iceGatheringState == "complete") {
              //Create a new offer with ICE candidates
              peerConnection.createOffer(function(offer) {
                  console.log("Offer with ICE candidates: " + offer.sdp);
                  signalingChannel.emit('offer', JSON.stringify(offer));
                  console.log("offer sent");
                  signalingChannel.on('answer', function(data){
                      console.log("Receive answer");
                      //The answer is set as the remote description for the offerer
                      peerConnection.setRemoteDescription(new RTCSessionDescription(JSON.parse(data)));
                      console.log("Set remote desc");
                      peerConnection.onaddstream = gotRemoteStream;
                      console.log("Add remote stream to peer connection");
                  });
              });
          }
      }
  
  }
  
  function answerer(){
      peerConnection = new webkitRTCPeerConnection(cfg);
      navigator.webkitGetUserMedia({'audio':true, video:true}, function (stream) {
          console.log("Got local audio", stream);
          video1.src = window.webkitURL.createObjectURL(stream)
          peerConnection.addStream(stream);
      }, 
      function ( err ) {
          console.log( 'error: ', err );
      });
      console.log("Answering");
      //Listen for offer
      signalingChannel.on('offer', function(data){
          console.log("Offer Received");
          //Set the remote description from caller's local description
          peerConnection.setRemoteDescription(new RTCSessionDescription(JSON.parse(data)));
          //Generate answer after getting the remote description
          peerConnection.createAnswer(function(sessionDescription) {
                  //Set local description
                  peerConnection.setLocalDescription(sessionDescription);
                  //The local desc will be the answer sent back to offerer
                  signalingChannel.emit('answer', JSON.stringify(sessionDescription));
                  console.log("Answer sent");
                  });
      });
  
  }
  
  function gotRemoteStream(event){
      video2.src = window.webkitURL.createObjectURL(event.stream);
  }
  </code></pre>