title: Node.js UDP for realtime multi-player game
body: <p>I am building a real-time multi-player browser-based game using node.js. At the moment I have the client send user input to the game logic server via socket.io and a snapshot of the game world sent back to the client for rendering.</p>
  
  <p>Below is a simplified version of the code. Is it possible to use UDP to send data from the browser-based client to the server and vice-versa? I know Node.js has a <a href="http://nodejs.org/api/dgram.html" rel="noreferrer">UDP module</a> but I am unsure how to implement it in this way. </p>
  
  <p>Any help would be appreciated. Thank you.</p>
  
  <p>Server:</p>
  
  <pre><code>var server = http.createServer(handler).listen(8888);
  var iosocket = io.listen(server);
  
  // request/response handler
  function handler(req, res){
  
      ...
  }
  
  iosocket.sockets.on('connection', function(socket){
      console.log("[x] Socket listener started");
  
      socket.on('msg', function(data){
          console.log( " [-] incoming message);
      });
  });
  
  ...
  
  iosocket.sockets.emit("message", msg);
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Test&lt;/title&gt;
          &lt;script type="text/javascript" src="/socket.io/socket.io.js"&gt;&lt;/script&gt;
          &lt;script type="text/javascript"&gt;
              socket.on('connect', function(){
               console.log("connected to server");
              });
  
              socket.on('message', function(message){
              console.log('received message');
              });
  
          &lt;/script&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;canvas id='canvas' width="400" height="400"&gt;Canvas&lt;/canvas&gt;
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>