I'm building a web app using AngularJS. The app needs to poll a URL that returns JSON data and make that data available to any part of the app. From what I've read so far, my best bet is to create a service that handles the polling and keeps its own internal cache of the JSON data, and then inject the service into any part of the app that wants to consult that data. What I'm lost on is how to actually go about doing that. The closest example I've found is this question, but it appears to be creating a service that's manually called by a specific controller (which is itself tied to a given route), whereas I want something that persistently runs in the background of the app forever regardless of what part of the app is active. Is this doable, or am I taking the completely wrong approach?
Here my solution:
app.factory('Poller', function($http, $timeout) {
var data = { response: {}, calls: 0 };
var poller = function() {
$http.get('data.json').then(function(r) {
data.response = r.data;
data.calls++;
$timeout(poller, 1000);
});
};
poller();
return {
data: data
};
});
(calls just to show that polling is been done)
http://plnkr.co/edit/iMmhXTYweN4IrRrrpvMq?p=preview
EDIT: As Josh David Miller suggested in comments, dependency on this service should be added in app.run block to ensure polling is done from start:
app.run(function(Poller) {});
And also moved scheduling of next poll after previous call finished. So there would not be "stacking" of calls in case if polling hangs for a long time.
Updated plunker.
Here is an angular poller service on Github that can be easily injected into your controller.
To install:
bower install angular-poller
.
Since you want to start a global polling service that runs in the background forever, you can do:
// Inject angular poller service.
var myModule = angular.module('myApp', ['poller']);
// The home/init controller when you start the app.
myModule.controller('myController', function($scope, $resource, poller) {
// Define your resource object.
var myResource = $resource(url[, paramDefaults]);
// Create and start poller.
var myPoller = poller.get(myResource);
// Update view. Most likely you only need to define notifyCallback.
myPoller.promise.then(successCallback, errorCallback, notifyCallback);
});
Now it will run in the background forever until you call myPoller.stop()
or poller.stopAll()
.
If you want to use the callback data of this poller in other controllers, you can simply do:
myModule.controller('anotherController', function($scope, $resource, poller) {
/*
* You can also move this to a $resource factory and inject it
* into the controller so you do not have to define it twice.
*/
var sameResource = $resource(url[, paramDefaults]);
/*
* This will not create a new poller for the same resource
* since it already exists, but will simply restarts it.
*/
var samePoller = poller.get(sameResource);
samePoller.promise.then(successCallback, errorCallback, notifyCallback);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With