title: Stream WebCam using socket.io
body: <p>I have been trying to implement a web application that will be able to handle following scenario:</p>
  
  <ul>
  <li>Streaming video/audio from a client to other clients (actually a particular set of them, no broadcasting) and server at the same time. The data source would be a webcam of the client.</li>
  <li>This streamed data has to be displayed in the real time on the other clients' browser <strong>and</strong> be saved on the server side for the 'archiving' purposes.</li>
  <li>It has to be implemented in node.js + socket.io environment.</li>
  </ul>
  
  <p>To put it in some more specific context... The scenario is that there is a guy that makes a kind of a room for the users that he chooses. After the chosen users join the room, the creator starts streaming video/audio from his/her built in devices (webcam). All of the guests receive the data in real time, moreover the data is being sent to the server where it is stored so it can be recovered after the stream and room get closed.</p>
  
  <p>I was thinking about mixing Socket.IO with WebRTC. In theory the combination of these two seem just perfect for the job.
  The Socket.IO is great for gathering specific set of users by assigning some sockets to a room and for signaling process demanded by the WebRTC.
  At the same time WebRTC is awesome for P2P connection between users gathered in the same room, it is also really easy to get access to the webcam and other built in devices that I might want to use.</p>
  
  <p>So yeah, everything is looking pretty decent in theory but I would really need to see some code in action so I could actually try to implement it on my own. Moreover, I see some issues:</p>
  
  <ul>
  <li>How do I save the stream that is sent by the P2P connection? Obviously server does not have access to that. I was thinking that I might treat the server as another 'guest', so it would be just another endpoint of the P2P connection with the creator of the room. Somehow it feels edgy, though.</li>
  <li>Wouldn't it be better to treat server as the middleman between the creator and the clients? At one point there might be some, probably insignificant, delay comparing to P2P but presumably it would be the same for all the clients. (I tried that but I can't get the streaming from webcam to the server done, that's however is the topic for a different question as I am having problems with processing the MediaStream)</li>
  </ul>
  
  <p>I was looking for some nice solutions but without any success. I have seen that there is this nice P2P solution made for socket.io: <a href="http://socket.io/blog/socket-io-p2p/" rel="noreferrer">http://socket.io/blog/socket-io-p2p/</a> . The thing is - I don't think it will handle the data stream well. The examples mention only simple chat app and I need something a little bit heavier than that.</p>
  
  <p>I would be really thankful for some specific examples, docs, whatever may lead me a little closer to the implementation of it as I really don't know how to approach it.</p>
  
  <p>Thanks in advance :)</p>