title: Batch requests in Node.js
body: <p>My program is communicating with a web service that only accepts ~10 requests per second. From time to time, my program sends 100+ concurrent requests to the web service, causing my program to crash.</p>
  
  <p>How do I limit concurrent requests in Node.js to 5 per second? Im using the request library.</p>
  
  <pre><code> // IF EVENT AND SENDER
      if(data.sender[0].events &amp;&amp; data.sender[0].events.length &gt; 0) {
  
  
          // FIND ALL EVENTS
          for(var i = 0; i &lt; data.sender[0].events.length; i++) {
  
              // IF TYPE IS "ADDED"
              if(data.sender[0].events[i].type == "added") {
                  switch (data.sender[0].events[i].link.rel) {
                      case "contact" :
                          batch("added", data.sender[0].events[i].link.href);
                          //_initContacts(data.sender[0].events[i].link.href);
                          break;
                  } 
              // IF TYPE IS "UPDATED"
              } else if(data.sender[0].events[i].type == "updated") {
  
                  switch (data.sender[0].events[i].link.rel){                     
                      case "contactPresence" :
                          batch("updated", data.sender[0].events[i].link.href);
                          //_getContactPresence(data.sender[0].events[i].link.href);
                          break;
                      case "contactNote" :
                          batch("updated", data.sender[0].events[i].link.href);
                          // _getContactNote(data.sender[0].events[i].link.href);
                          break;
                      case "contactLocation" :
                          batch("updated", data.sender[0].events[i].link.href);
                          // _getContactLocation(data.sender[0].events[i].link.href);
                          break;
                      case "presenceSubscription" :
                          batch("updated", data.sender[0].events[i].link.href);
                          // _extendPresenceSubscription(data.sender[0].events[i].link.href);
                          break;
                  }
              }
          };
  </code></pre>
  
  <p>And then the homegrown batch method:</p>
  
  <pre><code>var updated = [];
  var added = [];
  
  var batch = function(type, url){
      console.log("batch called");
  
  
      if (type === "added"){
          console.log("Added batched");
          added.push(url);
          if (added.length &gt; 5) {
              setTimeout(added.forEach(function(req){
                  _initContacts(req);
              }), 2000);
              added = [];
          }
      } 
      else if (type === "updated"){
          console.log("Updated batched");
          updated.push(url);
          console.log("Updated length is : ", updated.length);
          if (updated.length &gt; 5){
              console.log("Over 5 updated events");
              updated.forEach(function(req){
                  setTimeout(_getContactLocation(req), 2000);
              });
              updated = [];
          }
      }       
  };
  </code></pre>
  
  <p>And an example of the actual request:</p>
  
  <pre><code>var _getContactLocation = function(url){
      r.get(baseUrl + url, 
      { "strictSSL" : false, "headers" : { "Authorization" : "Bearer " + accessToken }}, 
          function(err, res, body){
              if(err)
                  console.log(err);
              else {
                  var data = JSON.parse(body);
                  self.emit("data.contact", data);
              }
          }
      );
  };
  </code></pre>