Home > Enterprise >  I want to dynamicly update the data that I get from an api but I don´t know how to do this
I want to dynamicly update the data that I get from an api but I don´t know how to do this

Time:11-07

I have a spring boot api that works with a postgresql database. My frontend is an angular application. I am working with the HttpClient. Now I always have to refrsh the website to fetch new data from my api. My question is: How can I fetch new data without refreshing the website??

Service:

getMaxId() : Observable<object> 
    {
        return this.client.get(url);
    }

Typscript file:

export class AktuelleWerteComponent implements OnInit {
  
  messwerte : any; 
  maxId : any;

  constructor(private service : MesswerteService) { }

  ngOnInit(): void {
    this.service.getMaxId().subscribe(data => {
      this.maxId = data;
    })
  }

}

HTML file:

<p style="font-size: 50px;">{{maxId.temperatur}}°C</p>

I hope someone can help me with my Problem.

CodePudding user response:

you didn't specify what is your needs exactly, so I will write here two options.

  1. WebSocket - this is a technology that let's you create an open connection between server and client, you can start with this Docks, this option is for an app that needs an open stream - like chat apps.
  2. you can put your code inside interval :
 `setInterval(() => { 
   // you API request
  }, 1000)

also, you can use interval() of rxjs, see this link

good luck :)

CodePudding user response:

One way to do it is by http polling.

https://www.learnrxjs.io/learn-rxjs/recipes/http-polling

  • Related