I have a service that uses Observable to get data from database. It seems that it gets data only once . I would that it listen when a user adds data to the DB then stream to Angular.
service to get all data
getAllProj(): Observable<NouveauProjet[]> {
return this.http.get<NouveauProjet[]>(
"http://127.0.0.1:8081/api/proj/projets"
);
}
service to add data
addProj(nouveauProjet: NouveauProjet): Observable<any> {
return this.http.post<NouveauProjet[]>(
"http://127.0.0.1:8081/api/proj/projets",
nouveauProjet
);
}
When I use my services I only subscribe to them
getAllProj() {
this.ajoutProj.getAllProj().subscribe(
response => {
console.log("hello"+response);
this.nouveauProjet=response;
},
error => console.log(error)
);
I thought that Observable still listen to server when a new data is added to DB but it's not the case, so How can I transform my observable to a data stream.
The short answer is Websockets
. This is actually a server side issue.
Your API will never 'push' data out when the Database is updated unless you have told it to do so, and the easiest way to accomplish that is with Websockets. I would provide an example implementation, but there are a lot of variables, especially if you are monitoring database updates and pushing information accordingly.
An Observable is a data stream. If you setup multiple subscriptions to the same API call in your service, when new data is pulled from the API every subscription will get the new values. The problem is that HTTP Requests are just that, 'requests' that originate from the client.