title: Webrtc and socket.io: createanswer() always has an error. Why?. My code connects two peers, but only one of them knows about it?
body: <p>I am using socket.io and serving on node.js. </p>
  
  <p>Here is code from the client side: </p>
  
  <pre><code>getUserMedia(constraints, handlemedia, error);
  function handlemedia(stream) {
    localStream = stream;
    attachMediaStream(localVideo, stream);
    socket = io.connect();
  
    document.getElementById("connect").addEventListener("click", function(e){
          remoteVideo = document.getElementById("remoteVideo");
          console.log("We know what socket is.", socket);
          servers = {"iceServers": [{ "url": "stun:stun.l.google.com:19302"}]};
         pc = new RTCPeerConnection(servers);
         pc.onicecandidate = console.log("GOT CANDIDATE");
         pc.addStream(localStream);
  
         pc.onaddstream = function(event) 
         {
             attachMediaStream(remoteVideo, event.stream);
         }
  
         function error()
         {
             console.log("An error occurred in the create offer section. ");
         }
         sdpconstraints = {'mandatory': {
         'OfferToReceiveVideo':true }};
  
  
          pc.createOffer(function(sessiondesc) 
          {
          console.log("Socket attemp 2: ", socket);
          pc.setLocalDescription(sessiondesc);
          console.log("Offer: ", sessiondesc);
          console.log("local description was set. Don't worry.");
          socket.emit("sessiondesc", sessiondesc);
          }, error, sdpconstraints);
  
  
          socket.on("sessiondesc", function(sessiondesc)
           {
          console.log("WE GOT THE FUNCTION ");
          sdpconstraints = {'mandatory': {
          'OfferToReceiveVideo':true }};
  
  
          console.log("Your partner created an offer for you.");
  
              pc.createAnswer(function(sessiondesc) 
              {
                  console.log("Socket attemp at create answer section: ", socket);
                  pc.setLocalDescription(sessiondesc);
                  console.log("Session description:  ", sessiondesc);
                  console.log("Local desc for create answer section was set.");
                  socket.emit("sessiondesc", sessiondesc);
              }, console.log("Error in create answer section occurred."), sdpconstraints);
  
           });
    }
  
  }
  </code></pre>
  
  <p>And here is the server code, using node.js and socket.io:</p>
  
  <pre><code>var io = require('socket.io').listen(app);
  io.sockets.on('connection', function(socket, webkitRTCPeerConnection,     client, id){
  
      socket.on("sessiondesc", function(sessiondesc)
      {
          console.log("Emitting session desc RIGHT NOW");
          socket.broadcast.emit("sessiondesc", sessiondesc);
      });
  
  
   });
  </code></pre>
  
  <p>So what happens is I open up two tabs in my browser, go to localhost on both tabs, and make sure I have console open to see the logs. </p>
  
  <p>Then I click connect on the first tab. It displays logs up to "local description set dont worry", but it does not display "WE GOT THE FUNCTION" stuff, which makes sense, because I haven't connected on the other tab yet. </p>
  
  <p>So I go to the other tab, and click connect. Now, on this second tab it only displays the stuff up to the local description set don't worry. So obviously the second tab isn't receiving anything from the first tab! Why? </p>
  
  <p>BUT when I go back to the first tab, it did receive something! It went through the "We got the function" part of my code. Then it does say "your partner created an offer for you"
  BUT the problem is, the next thing it says is "error in create answer section", so there was an error in createanswer! Why is this? Why did it call this error? </p>
  
  <p>It also doesn't display any remote videos on any of the tabs. </p>