1

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.

2 Answers 2

2

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.

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

6 Comments

I&#39;ll be interested for an example showing how to stream data in using Express js and mongo DB database. For example, a user that adds data then automatically send it to the client using get method
To be frank you won't find one. Express is a REST API meaning it only responds to requests. Look into Socket.IO. You need to subscribe to an open socket on the server capable of pushing data.
Your best bet is a 2-way implementation using Express (to respond to requests that require a lot of middleware), and socket.io as your "New Info" broadcast server. There are a lot of security issues with websockets, so do your due diligence and make sure your not broadcasting sensitive data.
Now I have maked a look at socket.io . Should I implement Socket.io for client ? or only make in server side a socket emit in post api when it is called and on socket in get api ?
Can you give me more informations to how to do that ?
|
0

Here is some example of how to create a data stream manually, so you can test whether your code is working fine or not, at your end.

let arr = [];
setInterval(() => arr.push((Math.random())), 1001);
return interval(2000).pipe(map(i => arr));

If the solution is not what you expect, then simply leave it for the beginners like me, who are learning rxjs, so they can improve their rxjs skills.

Comments

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.