3
I need to have a data list updated every 5/10 minutes, this data which is brought from a Spring Boot API with Mysql storage, through a request made by Angular. I would like to know some way to keep repeating this request and so updating the data.
3
I need to have a data list updated every 5/10 minutes, this data which is brought from a Spring Boot API with Mysql storage, through a request made by Angular. I would like to know some way to keep repeating this request and so updating the data.
5
You can use the library Rx-Polling, with it you can make periodic requests by setting an interval.
Follow an example:
import {Component} from '@angular/core';
import {Http} from '@angular/http';
import 'rxjs/Rx'; // Não se esqueça de importar adequadamente :)
import {Observable} from 'rxjs/Rx';
@Component({
selector: 'app',
template: `
<ul>
<li *ngFor="let dado of dados">{{dado.meuDado}}</li>
</ul>
`
})
export class MyApp {
private dados = [];
observableDados: any;
constructor(http: Http) {
this.observableDados = Observable.interval(5000) // Intervalo de polling (5 segundos nesse caso)
.switchMap(() => http.get('http://minhaapirest.com/dados/')).map((data) => data.json()) // Requisição para sua API
.subscribe((data) => {
this.dados = data;
console.log(data); // Log para demonstração :)
});
}
ngOnDestroy() {
this.observableDados.unsubscribe();
}
}
Browser other questions tagged angular spring-boot
You are not signed in. Login or sign up in order to post.
It worked perfectly, I just made a small change regarding the service used. I used the Httpclient module and it also worked.
– Edson Camargo
Is Observable.interval() good practice? Is there a problem in using it? For example, performance?
– Edson Camargo
It is a library resource, usually works as a thread. Regarding performance, I think it depends a lot on the operation you perform, for example, you are consulting a URL with a return of 50MB, every 1 second, it will be heavy (not that it impacts performance, sometimes not :) ) but it will measure the cost of the operations performed. Leave to worry about performance when you actually have performance issues
– nullptr
One last question, let’s assume that I have a simple chat (only texts) and would like to leave it in real time (it doesn’t have to be exactly in Altime), I could use this same library to be looking for updates of the messages by the api?
– Edson Camargo
It works, but in this scenario the ideal would be for you to use websockets, such as lib socket. for example
– nullptr