1

I am searching solution for this question more than 3 day and can't find anything..

I have ionic3 App and working width Http POST requests. I am sending requests to my php server and geting data..

My data-api.ts (provider)

  public getNotifications(token){
    return this.http.post(this.sharedVars.getApi(),"action=messages/notification&token="+token, this.options
    ).map(res => res.json());
  } 

profilePage.ts

notifications() {
    this.api.getNotifications(this.user.token).subscribe(
      data => {
        if(data.err == 0){
          this.notifications = data.data;
        }
      },
      err => {
          console.log(err);
      }
  ); 
}

This is working functions and I am getting right output (1) when click this function. but on x action on my server notification count will changed to 2, 3, 4 etc.. and I want load this function not on click, but on page load. so If this.notifications have new value I want change value live (like as firebase)

Example 2:

I have send message action in my data-api.ts (provider)

  public sendMessage(token, to, message, attachment){
    return this.http.post(this.sharedVars.getApi(),"action=messages/send&token="+token+"&to="+to+"&message="+message+"&attachment="+attachment, this.options
    ).map(res => res.json());
  }

and also have function to get this messages.

  public getActivity(token){
    return this.http.post(this.sharedVars.getApi(),"action=messages/getActivity&token="+token, this.options
    ).map(res => res.json());
  } 

so if I am making post request to sendMessage then I want listen live getActivity action and load new message in my page but not reload.. like as firebase..

I hope this question is clear. because I am not english speaker and tryng to find solution. Tanks

Georgia
  • 27
  • 4

1 Answers1

0

Listening actively to live events is not possible with a single HTTP request in angular.

However you might look into eventSources. Look at this question for using with angular 2+ : Creating an RxJS Observable from a (server sent) EventSource

Niles Tanner
  • 3,911
  • 2
  • 17
  • 29