2

I would like to retrieve table data from a REST backend server with angularjs. The data changes by the second. I would like to use angularjs to refresh the data as it changes in real-time. How can this be done? Do I force angularjs to make ajax calls at regular time intervals to refresh the data display?

2
  • You should learn firebase. firebase.com/quickstart/angularjs.html Commented Feb 10, 2014 at 14:24
  • as has been answered, you should be able to use $timeout on your $resource's get/query request Commented Feb 10, 2014 at 14:38

2 Answers 2

2

Yeah with REST there's no other way then polling. To refresh the data itself in the browser view you can use $rootScope.$apply() in the service (I presume that you're using a service to get the data), first you have to inject the dependency of $rootScope of course.

EDIT Actually you shouldn't use $rootScope.$apply(), because it can lead to ugly $digest in progress errors. Instead use

$timeout(function(){
  // set data here...
})

A tip for improvement if you're not happy with the polling and if you have programmed the backend or are in position to change it, is: Try to use WebSockets:

  • It gets rid of the polling, because then your browser can communicate directly to the server.
  • It has less overhead.
  • It is supported in the major browsers and you can use libraries with fallback mechanisms like SocketIO

The server can then always push the data right then, when it's available. The server-side implementation depends on the backend you are using, but most backend frameworks/servers nowadays also support websockets.

Sign up to request clarification or add additional context in comments.

Comments

2

You have to either use the poll or push strategy. However, since you already know that the resource changes regularly, it should be enough to setup a recurring timeout so that your application polls the resource on the REST server every second. Once it is retrieved, AngularJS updates the view. So, yes you have to force AngularJS to make calls to the service.

2 Comments

May I ask if there are sample source code for doing this? I am very new to even javascript, much less AngularJS.
I'd suggest to take some time to know how Angular works. There are several tutorials on the Net to get you started. There is an Angular wrapper for window.setInterval and it is easy to use AngularJS $interval

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.