Search code examples
angularjslong-polling

Writing an angularJS poller


I'm trying to write an angularJS service for long polling in my application, using $http.

Here's my code :

app.factory('Poller', function($http, $timeout){
    var poll = function(http, tick){
        http.then(function(r){
            $timeout(poll, tick);
            return r.data;
        });
    };

    return{
        poll: poll
    };
});

The basic idea would be to inject this service whenever I need polling on a $http call. I'm using it inside a controller :

app.controller('myCtrl', function($scope, $http, Poller){
    $scope.polledVar = Poller.poll($http.get('api/getVar'), 1000);
});

When using this code I get the following error :

TypeError: Cannot call method 'then' of undefined

Solution

  • Even if I don't understand the design (overhead design imo) here is it:

    app.factory('Poller', function($http, $timeout,$q){
                    var poll = function(http, tick){
                       return http.then(function(r){
                            var deferred = $q.defer();
                            $timeout(function(){
                                deferred.resolve(r);   
                            }, tick);
                            return deferred.promise;
                        });
                    };
    
                    return{
                        poll: poll
                    };
                });
    

    You could simply pass the url like Poller.poll('api/getVar', 1000);

    UPDATE

    just to play around :) and following https://stackoverflow.com/a/16520050/356380

    var app = angular.module('myModule', []);
    
                app.factory('Poller', function($http,$q){
                   return {
                        poll : function(api){
                            var deferred = $q.defer();
                            $http.get(api).then(function (response) {
                                    deferred.resolve(response.data);
                            });
                            return deferred.promise;
                        }
    
                    }
                });
                app.controller('myCtrl', function($scope, $http,$filter ,Poller){
                    //Just to start
                    $scope.myts = Poller.poll('mytest.php');
                    $scope.mydate = $scope.myts.then(function(data){
                        return $filter('date')(data,'yyyy-MM-dd HH:mm:ss Z'); 
                    }); 
                    var Repeater = function () {
                        $scope.$apply(function () {
                            $scope.myts = Poller.poll('mytest.php');
                            $scope.mydate = $scope.myts.then(function(data){
                                return $filter('date')(data,'yyyy-MM-dd HH:mm:ss Z'); 
                            });
                        });
                    };
                    var timer = setInterval(Repeater, 1000);             
               });
    

    mytest.php

    echo time()*1000;