title: Node.js: Receiving too many UDP messages at a time, losing them
body: <p>My node server receives about <strong>400 UDP messages</strong> in one second, and it all <strong>works</strong>, and I am able to process all 400 of them.</p>
  
  <p>However, when I start to receive about <strong>700 UDP messages</strong> in one second, <strong>I lose 2-20</strong> of the messages, and they never get parsed :(</p>
  
  <p>I have thought about some <strong>options</strong> here:</p>
  
  <ol>
  <li><strong>Create a queue</strong> of all the socket messages, then consume one-by-one,
  although I'm not sure how to implement this
  <ul>
  <li>Can't figure out how to implement</li>
  </ul></li>
  <li>Find a <strong>setting</strong> in <strong>Node</strong> / Express / dgram socket where i can increase the memory size / buffer size, something like that
  <ul>
  <li>I couldn't find any settings like this, though :(</li>
  </ul></li>
  <li><strong>Use a different UDP receiver</strong>, stop using node's build in socket UDP receiver
  <ul>
  <li>Didn't find other receivers</li>
  </ul></li>
  </ol>
  
  <p>Here's what my UDP sender looks like:</p>
  
  <pre><code>var dgram = require("dgram");
  var udpserver = dgram.createSocket("udp4");
  var seatStateStore = require("./SeatStateStore");
  
  udpserver.on("message",
          function (msg, rinfo)
          {
          seatStateStore.parseMessage(msg.toString());
      });
  </code></pre>
  
  <p>Anyone have any ideas? I couldn't figure out any of the 3 options :/ Can someone help me out?</p>
  
  <p>Node v0.10.29</p>
  
  <p>Express v3.14.0</p>
  
  <p>===============================</p>
  
  <h1>UPDATE / SOLUTION</h1>
  
  <p>Here's the code I ended up using (slightly modified @RoyHB 's solution):</p>
  
  <pre><code>var dgram = require("dgram");
  var udpserver = dgram.createSocket("udp4");
  var seatStateStore = require("./SeatStateStore");
  var Dequeue = require('dequeue');
  var FIFO = new Dequeue();
  
  fetcher();
  
  udpserver.on("message",
          function (msg, rinfo)
          {
             FIFO.push(msg.toString());
          });
  
  udpserver.bind(43278);
  
  function fetcher () {
      while (FIFO.length &gt; 0) 
      {
          var msg = FIFO.shift();
          seatStateStore.parseMessage(msg);
      }
      setImmediate(fetcher); //make this function continuously run
  }
  </code></pre>