angular-http-polling

An AngularJS service that allows for continuously polling an remote server.


Keywords
angularjs, service, poll, long-polling
License
MIT
Install
bower install angular-http-polling#v1.0.0

Documentation

angular-http-polling

An AngularJS service that allows for continuously polling a remote server.

Requirements

  • AngularJS v1.5.8+ is currently required.

Installation

Install this plugin with:

$ bower install angular-http-polling

Usage

Injection

Add ngHTTPPoll as a dependency to your module and inject $httpoll:

angular.module('YourModule',['ngHTTPPoll'])
    .controller('YourController', ['$httpoll'], function ($httpoll){
        // ...
    })

Config Options

$httpoll wraps Angular's $http methods, polling the endpoint multiple times, based on the kind of behavior you configure. The API is the same for all $http methods, except that it accepts additional config variables:

  • retries [integer] The maximum number of retries that the poller will attempt until it receives a status code in either the successRange or errorRange (default: 50)
  • delay [integer] Time (in milliseconds) to delay the next retry after a response is received (default: 100)
  • retryOnError [boolean] Polling continues if an error response is received (default: true)
  • timeout [integer] Timeout for polling - no further retries will be attempted. A false-y value means that the poller will never timeout. (default: false)
  • successRange [array]: A range of HTTP status codes that the poller should interpret as a success (default: [200, 201])
  • errorRange [array]: A range of HTTP status codes that the poller should interpret as errors (default: [400, 599])

Examples

// A GET request that retries 50 times, with 200ms between each response/request
$httpoll.get('/some_resource', {retries: 50, delay: 20})

// A POST request that continues to retry if the server returns an error
$httpoll.post('/some_resource', {resource: {...}}, {retryOnError: true})

Development and Testing

  1. Install Node.js and NPM (should come with)

  2. Install local dependencies:

    $ npm install
    $ bower install
  3. Run tests:

    $ karma start