title: JavaScript Node Js Event Loop my own events
body: <p>I'm using a raspberry pi to read data from hardware and set up things on the server side.</p>
  
  <p>I want to be able to add my own event list to Node JS event loop. For instance, every 20min I would like to fire off a function, so I need to check every Node JS tick where 20mins is up.</p>
  
  <p>I was thinking that I would have my own table set up in MongoDB called event list, or have one created in an object in my server side code.</p>
  
  <p>then when the server has been initialised add this function to the Node JS event loop:</p>
  
  <pre><code>function processEventQueue() {
     var eventQueue = events /* either database query or object in server side */;
  
     // check the values against each object to make sure if we've hit anything to action it.
  
     async.each(events, function (item) {
        // execute some action here
     });
  
     // could use a for loop because each event from the event queue aren't mutually exclusive
     // i.e can happen at the same time
  }
  </code></pre>
  
  <p>What I need to know:</p>
  
  <ul>
  <li>Is this going hold up the Node JS event loop?</li>
  <li>Where is the best place to hold the events?</li>
  <li>What is the best way to iterate over the events?</li>
  </ul>
  
  <p><em><strong>EDIT ============================================</em></strong></p>
  
  <pre><code>function processEventQueue() {
     // do some work here
  
     setImmediate(processEventQueue.bind(this));
  }
  </code></pre>
  
  <p>This function will call itself efficiently each time, however, once in this loop, it is trying to get it to stop. Secondly after careful consideration, there shouldn't be any database calls in the event queue, because each <code>process.nextTick()</code> happens so quickly that you could end up DoS'ing the database.... which isn't good.</p>
  
  <p>In the <code>processEventQueue</code> there needs to be some kind of prevention of executing the same event at the same time if the other one hasn't finished executing.</p>