AngularJS HTTP Batcher
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
-
Module 'jcs.angular-http-batch' is not available! You either misspelled the module name or forgot to load it. If error is for a module this means the module itself is not loaded.
cause The `angular-http-batch.min.js` script file was not included in the HTML, or the module name `'jcs.angular-http-batch'` was misspelled when declaring application dependencies.fixEnsure `<script src="path/to/angular-http-batch.min.js"></script>` is present in your HTML *after* AngularJS itself, and verify `angular.module('myApp', ['jcs.angular-http-batch'])` has the correct module name. -
Unknown provider: httpBatchConfigProviderProvider
cause The `httpBatchConfigProvider` was requested in an Angular configuration block, but the `jcs.angular-http-batch` module has not been loaded or correctly declared as a dependency of the main application module.fixVerify that `angular.module('myApp', ['jcs.angular-http-batch'])` correctly lists the batcher module and that its JavaScript file is loaded. -
Cannot read property 'setAllowedBatchEndpoint' of undefined
cause The `httpBatchConfigProvider` was not successfully injected into the config block, likely due to a misspelling or the module not being loaded.fixCheck the spelling of `httpBatchConfigProvider` in your config function's argument list and array declaration. Also, ensure the main Angular module correctly lists `'jcs.angular-http-batch'` as a dependency.
Warnings
- breaking This library is specifically designed for AngularJS (Angular 1.x). It is not compatible with Angular 2+ applications. For Angular 2 and later, use `ngx-http-batcher` instead.
- gotcha The library will only batch requests if they are directed at an endpoint whose root URL has been explicitly registered with `httpBatchConfigProvider.setAllowedBatchEndpoint`.
- gotcha Batching is asynchronous and occurs after a short `batchRequestCollectionDelay`. Requests made within this delay window will be grouped. Requests outside this window or that exceed `maxBatchedRequestPerCall` will be sent individually or in new batches.
- gotcha Certain HTTP verbs are ignored by default (e.g., 'HEAD'). If you need to batch these or other specific verbs, you must override the `ignoredVerbs` option.
Install
-
npm install angularjs-http-batcher -
yarn add angularjs-http-batcher -
pnpm add angularjs-http-batcher
Imports
- jcs.angular-http-batch
import { httpBatch } from 'angularjs-http-batcher';angular.module('myApp', ['jcs.angular-http-batch']); - httpBatchConfigProvider
app.config(['HttpBatchConfigProvider', function(HttpBatchConfigProvider) { ... }]);app.config(['httpBatchConfigProvider', function(httpBatchConfigProvider) { ... }]); - httpBatchAdapter
{ adapter: HttpBatchAdapter }{ adapter: 'httpBatchAdapter' }
Quickstart
<!-- 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>