简体   繁体   English

具有自定义通知功能的Angular 4项目

[英]Angular 4 project with custom notifications feature

I built a notification feature that will trigger after a particular event and add a new notification to an API endpoint. 我构建了一个通知功能,该功能将在特定事件后触发,并将新的通知添加到API端点。 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. 我读取通知的方式是每3秒向API端点发送一次get请求,然后重新分配未读取的通知号。 This number then gets rendered in the UI. 然后,该数字将在UI中呈现。

What are the downsides of doing this? 这样做的缺点是什么? Is there a better way to do this without constantly hitting the API? 有没有更好的方法可以做到这一点而又不需要不断地访问API?

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

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 :-) 希望这有所帮助 :-)

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 { ..... } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM