periodic refresh using xml http get request

13 مرّة مشاهدة
التخطي إلى أول رسالة غير مقروءة

Stéphane Ancelot

غير مقروءة،
26‏/07‏/2017، 8:20:21 ص26‏/7‏/2017
إلى Angular and AngularJS discussion
Hi,

I have a view that will need to refresh some data on display every 300ms.

I will get these data with an xml http get request.

what sounds to be the best method to do the refresh loop (getting data and refresh ?) .

Regards
S.Ancelot



Zlatko Đurić

غير مقروءة،
27‏/07‏/2017، 5:02:08 ص27‏/7‏/2017
إلى Angular and AngularJS discussion
 This is really a broad question. But speaking in generic terms, it would be something like this:

- have a service which exposes a shared observable, say data$;
- the service has the method to fetch the remote data and update the shared observable above, say getRemoteData()
- that service Observable.interval() which calls the method to fetch the remote data

- have your component that displays the data
- have the component subscribe to the data observable from the service.

Also, depending on your views, maybe you don't need this remote data on all views. In that case make sure that you inject the service only in the view that needs it. Or at least don't start the Observable.interval() automatically, but rather kick it off from the actual component (and stop the polling if the component dies).

That's fairly simple. Now, fetching remote data, processing it and updating views may take more than 300 milliseconds, depending on what the data is and how big is the view. You probably also need a safeguard which can ensure that if the previous request is not complete, don't start a new one. And also probably that if it fails to fetch fresh data in several seconds, you kick off a new one anyway. That might be a way to do this.

Now, if you have control of the remote endpoint, did you consider a websocket or an SSE (server-sent events) endpoint? It could have less overhead.

الرد على الكل
رد على الكاتب
إعادة توجيه
0 رسالة جديدة