Santosh Gonzalez
Santosh Gonzalez

Reputation: 1

How to send http request in a timely basis and keep updated data in My Angular/ ionic components

I have a simple driver ionic/angular application that needs to regularly retrieve latest bookings updates from a server.

The server have no SignalR or similar technology implemented, so I have to make an http request to fetch the data from the server ever 10 second ...

I also have to have a central ionic/angular service that have a local variable which holds a updated version of the bookings (every 10 second). Let's call it UpdateBookings of type booking[]

other pages like bookings-list and booking-details, must be notified every time there is a change in the data in the variable UpdateBookings.

Maybe via an RxJs observable, subject or BehaviorSubject ...

What is the best way to implement such scenario?

Upvotes: 0

Views: 256

Answers (1)

zainhassan
zainhassan

Reputation: 1780

you can use rxjs interval for calling api every 10 sec

private _updateBookings = new BehaviorSubject<booking[]>([]);
updateBookings = this._updateBookings.asObservable();

getBookings(){
    //return http.get().pipe(map((bookings:booking[])=>{
        this._updateBookings.next(bookings);
    })
}

Then in your component you can call & subscribe updateBookings

 interval(10000).subscribe()=>{
    this.service.getBooking();
});

this.service.updateBookings.subscribe((bookings:booking[])=>{
    //do some stuff
})

Upvotes: 1

Related Questions