title: Live Video Stream on a Node.js Server
body: <p>I have been researching this a lot but am frustrated as I feel like the solution <strong>should be</strong> simple though I know wont be. Ideally i'd just want to use node to host the server, webrtc getusermedia to get the live stream on the local client and use something like socket.io to send the stream to the server and then the server would broadcast the stream to the remote client; as if it was a simple messaging chat app. </p>
  
  <p>Just thinking about this some more it seems as an approach this simple would be impossible because a live video requires continuous large amounts of data to be sent, which does not equate to sending a single message or even file after an event (send button pressed). </p>
  
  <p>Maybe I am wrong however, can a live video stream app follow the same structure of a node/socket.io messenger app? Would you send the media object returned from getUserMedia, the blob, some binary data some how (I've tried all of these but perhaps not correctly). </p>
  
  <p>The ideal goal would be an app that uses as little <em>extra fluff</em> as necessary, as little npm installs, as little extra javascript libraries, or little worrying about encoding/decoding or whatever the hell ICE or STUN are. Is there any way this is possible or am I asking for too much?</p>
  
  <p><strong>Ideal Client</strong></p>
  
  <pre><code>    var socket = io();
      var local = document.getElementById("local_video");
      var remote = document.getElementById("remote_video");
  
      // display local video
      navigator.mediaDevices.getUserMedia({video: true, audio: true}).then(function(stream) {
        local.src = window.URL.createObjectURL(stream);
        socket.emit("stream", stream);
      }).catch(function(err){console.log(err);});
  
      // displays remote video
      socket.on("stream", function(stream){
        remote.src = window.URL.createObjectURL(stream);
  
      });
  </code></pre>
  
  <p><strong>Ideal Server</strong></p>
  
  <pre><code>var app = require("express")();
  var http = require("http").Server(app);
  var fs = require("fs");
  var io = require("socket.io")(http);
  
  app.get('/', onRequest);
  http.listen(process.env.PORT || 3000, function() {
      console.log('server started');
  })
  
  //404 response
  function send404(response) {
      response.writeHead(404, {"Content-Type" : "text/plain"});
      response.write("Error 404: Page not found");
      response.end();
  }
  
  function onRequest(request, response) {
    if(request.method == 'GET' &amp;&amp; request.url == '/') {
      response.writeHead(200, {"Content-Type" : "text/html"});
      fs.createReadStream("./index.html").pipe(response);
    } else {
      send404(response);
    }
  }
  
  io.on('connection', function(socket) {
    console.log("a user connected");
    socket.on('stream', function(stream) {
      socket.broadcast.emit("stream", stream);
    });
    socket.on('disconnect', function () {
      console.log("user disconnected");
    });
  });
  </code></pre>
  
  <p>This is the broken app in action : <a href="https://nodejs-videochat.herokuapp.com/" rel="noreferrer">https://nodejs-videochat.herokuapp.com/</a></p>
  
  <p>This is the broken code on github: <a href="https://github.com/joshydotpoo/nodejs-videochat" rel="noreferrer">https://github.com/joshydotpoo/nodejs-videochat</a></p>