0

I built a notification feature that will trigger after a particular event and add a new notification to an API endpoint. The way that I am reading the notification is by sending a get request to the API endpoint every 3 seconds and re-assigning the unread notifications number. This number then gets rendered in the UI.

What are the downsides of doing this? Is there a better way to do this without constantly hitting the API?

this.subscription = Observable.interval(3000).subscribe(() => {
   //get request to notification endpoint
}
Kenzo
  • 367
  • 2
  • 5
  • 21

2 Answers2

1

As I see it, your way is perfectly valid. Doing polling like you do, you have complete control of the process and it is also really simple.

Your other options might be to implement server push in some way:

Hope this helps a little :-)

Heehaaw
  • 2,677
  • 17
  • 27
1

you can add a condition to detect user's idle time, reduce unnecessary requests

this.subscription = Observable.interval(3000).subscribe(() => {
    if(this.isIdle()){
       return;
    }
   //get request to notification endpoint
}

private isIdle() :boolean {
   .....
}
Chunbin Li
  • 2,196
  • 1
  • 17
  • 31