title: Connect to a room when arriving with WebRTC.io and NodeJS
body: <p>I'm actually working on an application based on NodeJS, ws, WebRTC.io and socket.io. The aim of this application is to broadcast the video of an Host to multiple attendees.</p>
  
  <p>The host and all the attendees load the web application, then the host launch his video and stream it to all the attendees. For this case I don't have any problem, but if the host launch his video, then an attendee join the room, this attendee is not able to retrieve the stream, and I don't how to get this stream.</p>
  
  <p>I've tried many solution.</p>
  
  <h2>1st solution</h2>
  
  <p>When an attendee arrive in the room, he send a message via socket.io to the Host which send back his stream. The problem is that the stream arrive to the attendee as an Object and not as a MediaStream, so I can't use it.</p>
  
  <h2>2nd solution</h2>
  
  <p>When an attendee arrive in the room, he send a message via socket.io to the Host which send back a blob created with <code>window.webkitURL.createObjectURL(rtc.streams[0])</code>, then I use this blob as the video source for the attendee, but I obtain a 404 error.</p>
  
  <p>Here is the code for both solutions :</p>
  
  <pre><code>var isStreamCreated = false;
  
  function startVideoLive() {
      var video = document.getElementById('video');
      //Test if peer connection is available for the browser
      if (PeerConnection) {
          if (getRole() == 1) {
              rtc.createStream({
                  "video": true,
                  "audio": true
              }, function (stream) {
                  video.src = URL.createObjectURL(stream);
                  video.play();
                  isStreamCreated = true
              });
  
          }
      }
      if (isConnected != true) {
          var room = '1234';
          rtc.connect('ws://' + server + ':8443', room);
          isConnected = true;
      }
  }
  
  function setAttendeeWebRTC() {
      if (getRole() == 0) {
          var message = {
              type: "getStreamInfos",
              URL: null,
              stream: null
          }
  
          socket.emit('message', message);
  
          rtc.on('add remote stream', function (stream, socketId) {
              rtc.attachStream(stream, 'video');
              isStreamCreated = true;
          });
      }
  }
  
  startVideoLive();
  setAttendeeWebRTC();
  
  function onGetStreamInfos(message) {
      if (getRole() == 1 &amp;&amp; isStreamCreated == true) {
  
          message.URL = window.webkitURL.createObjectURL(rtc.streams[0]);
          message.stream = rtc.streams[0];
  
          socket.emit('message', message);
      } else if (getRole() == 0 &amp;&amp; isStreamCreated == false &amp;&amp; message.URL != null &amp;&amp; message.stream != null) {
  
          document.getElementById('video').src = message.URL;
          // OR
          rtc.attachStream(message.stream, 'video')
          isStreamCreated = true;
  
      }
  }
  </code></pre>
  
  <p>getRole() is used to know if the user connected is an attendee (0) or the host (1).</p>
  
  <p>In the server side I just broadcast the websocket message, and add webrtc.io (and many other things, but not usefull in this case).</p>
  
  <p>Here is the errors for the 2 case :</p>
  
  <ul>
  <li><p>With the blob : GET blob:http%3A//128.1.222.159%3A8081/881928b6-70d3-4054-b15e-88d635a2125f 404 (Not Found)</p></li>
  <li><p>With the stream : Uncaught TypeError: Type error<br>
  rtc.attachStream<br>
  onGetStreamInfos<br>
  (anonymous function)<br>
  EventEmitter.emit socket.io.js?<em>=1375350434038:633<br>
  SocketNamespace.onPacket socket.io.js?</em>=1375350434038:2248<br>
  Socket.onPacket socket.io.js?<em>=1375350434038:1930<br>
  Transport.onPacket socket.io.js?</em>=1375350434038:1332<br>
  Transport.onData socket.io.js?_=1375350434038:1303<br>
  WS.open.websocket.onmessage</p></li>
  </ul>
  
  <p>If someone has any ideas on how I can deal with my problem, I would thank in advance.</p>
  
  <h2>EDIT 1 :</h2>
  
  <p>I found how to figured out my problem, but there is a new problem. Here is my partial solution : </p>
  
  <pre><code>function onGetStreamInfos(message) {
      if (getRole() == 1 &amp;&amp; isStreamCreated == true) {
  
          rtc.createPeerConnections();
          rtc.addStreams();
          rtc.addDataChannels();
          rtc.sendOffers()
  
      } else if (getRole() == 0 &amp;&amp; isStreamCreated == false) {
  
          isStreamCreated = true;
  
      }
  }
  </code></pre>
  
  <p>But now, when an attendee join the room the other attendees lost the video. I think it's because normally I shouldn't create peer a peer connection between the new attendee and the old one.</p>
  
  <p>I'm not sure if it's very clear, but if you need more information don't hesitate to ask, and if I finally find how to fix my problem I'll give the solution here.</p>
  
  <h2>EDIT 2 :</h2>
  
  <p>By removing <code>rtc.createPeerConnections();</code> and <code>rtc.addDataChannels();</code> it seems to work in most of the case (I didn't test so much case).</p>