AngularJS HTTP Batcher

1.13.0 · maintenance · verified Wed Apr 22

The angularjs-http-batcher library provides a transparent mechanism to batch multiple outgoing HTTP requests into a single network call within AngularJS (Angular 1.x) applications. This significantly improves application performance by reducing the number of round trips to the server. The library intercepts standard `$http` requests and, if destined for a configured batch endpoint, combines them according to the HTTP 1.1 batch specification, which is commonly used by .NET Web API and Java servers. It also includes an adapter for Node.js multifetch and aims to support Facebook's batching protocol. Version 1.13.0 is the current stable release. Its key differentiator is the 'transparent' nature of batching, requiring minimal configuration and no specific batching calls from the developer, making it simple to integrate into existing AngularJS applications. This package is specifically for AngularJS and is not compatible with Angular 2+.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to include the `angularjs-http-batcher` library, configure a batch endpoint, and show how typical `$http` GET requests are transparently batched.

<!-- Include the AngularJS library first -->
<script src="node_modules/angular/angular.min.js"></script>
<!-- Then include the http-batcher library -->
<script src="node_modules/angularjs-http-batcher/dist/angular-http-batch.min.js"></script>

<script>
  angular.module('myApp', ['jcs.angular-http-batch'])
    .config([
      'httpBatchConfigProvider',
      function (httpBatchConfigProvider) {
        httpBatchConfigProvider.setAllowedBatchEndpoint(
          // Root endpoint URL of your API
          'http://api.myapp.com',
          // The specific endpoint that accepts batch requests
          'http://api.myapp.com/batch',
          // Optional configuration parameters
          {
            maxBatchedRequestPerCall: 20,
            minimumBatchSize: 2,
            batchRequestCollectionDelay: 100,
            ignoredVerbs: ['HEAD', 'OPTIONS'], // Do not batch these verbs
            sendCookies: true,
            enabled: true,
            adapter: 'httpBatchAdapter' // Defaults to HTTP 1.1 adapter
          }
        );
      }
    ]);

  angular.module('myApp')
    .controller('MyController', ['$http', function($http) {
      console.log('Fetching user data...');
      $http.get('http://api.myapp.com/users/1').then(res => console.log('User 1:', res.data));
      $http.get('http://api.myapp.com/users/2').then(res => console.log('User 2:', res.data));
      $http.get('http://api.myapp.com/products/10').then(res => console.log('Product 10:', res.data));
      // These three GET requests to 'http://api.myapp.com' will be batched if within the delay window
    }]);

  // Manually bootstrap the Angular app
  angular.element(document).ready(function() {
    angular.bootstrap(document, ['myApp']);
  });
</script>
<div ng-app="myApp" ng-controller="MyController">
  <h1>HTTP Batching Demo</h1>
  <p>Check your browser's network tab for batched HTTP requests.</p>
</div>

view raw JSON →