Endless polling with Angular 4 and Http observables

I am trying to build an infinite poll in my Http service because I am building a dashborad king that looks at the data coming from the server, here is my code that almost works (in my console I can see the Json is being sent but it does not reflect my opinion. .. I would like to add to my users: Observable

const usersURL = 'http://my.super.servor.php'

@Injectable()
export class UserService {

 users: Observable<User[]>

   constructor (public http:Http) {
     this.users = http.get(usersURL)
              genre mobile ou autre
              .map(res => [res.json()]);

        let i = this.users.subscribe(
          usersURL => console.log(usersURL),
         () => {}, // Here we catch up errors
         () => console.log("completed!") // Here we catch up if its completed
        )

    // Here where I'm trying to do the polling every 5 secondes
    let tick$ = Observable.interval(5000);

    let response$ = 
      tick$
          .flatMap(() => http.get(usersURL))
          .map(res => [res.json()]);

    let stockPoller = response$.subscribe(res => console.log(res));
  }

      

+3


source to share


1 answer


You simply assign your poll as Observable to this.users

:



this.users = tick$.flatMap(() => http.get(usersURL)).map(res => [res.json()]);

      

+5


source







All Articles