title: node.js listen for UDP and forward to connected http web clients
body: <p>I'm new to node.js, so forgive the ignorance if this is simple.</p>
  
  <p>What I want to do is setup a simple node.js http server to which a web-client connects. I also want the node.js server to act as a UDP listener on a separate port, on which it will receive JSON payloads from some other application. I want the node.js server to then forward these JSON payloads immediately to one or more of the connected web-clients.</p>
  
  <p>I got this far from some initial googling around:</p>
  
  <ol>
  <li><p>Create a simple node.js http server that responds with a static html page:</p>
  
  <pre><code>//Initialize the HTTP server on port 8080, serve the index.html page
  var server = http.createServer(function(req, res) {
      res.writeHead(200, { 
          'Content-type': 'text/html'});
          res.end(fs.readFileSync(__dirname + '/index.html'));
      }).listen(8080, function() {
          console.log('Listening at: 127.0.0.1 8080');
      }
  );
  </code></pre></li>
  <li><p>Initialize a UDP server on a separate port:</p>
  
  <pre><code>//Initialize a UDP server to listen for json payloads on port 3333
  var srv = dgram.createSocket("udp4");
  srv.on("message", function (msg, rinfo) {
    console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
    io.sockets.broadcast.emit('message', 'test');
    //stream.write(msg);
    //socket.broadcast.emit('message',msg);
  });
  
  srv.on("listening", function () {
    var address = srv.address();
    console.log("server listening " + address.address + ":" + address.port);
  });
  
  srv.bind(5555);
  </code></pre></li>
  <li><p>Use socket.io to establish a live connection between web-client and server:</p>
  
  <pre><code>//this listens for socket messages from the client and broadcasts to all other clients
  var io = require('socket.io').listen(server);
  io.sockets.on('connection', function (socket) {
      socket.on('message', function (msg) {
        console.log('Message Received: ', msg.data.skeletons[0] ? msg.data.skeletons[0].skeleton_id : '');
        socket.broadcast.emit('message', msg);
      }
    );
  });
  </code></pre></li>
  </ol>
  
  <p>I guess my problem is I don't know how to bridge 2 and 3, to get the received UDP packets broadcasted to the connected socket.io clients. Or perhaps there's a simpler, more elegant way of doing this? I found the documentation for socket.io to be lacking...</p>
  
  <p>EDIT: thanks to the person that fixed the code formatting</p>