客户端通知,我应该使用 AJAX 推送还是轮询? [英] Client notification, should I use an AJAX Push or Poll?

查看:34
本文介绍了客户端通知,我应该使用 AJAX 推送还是轮询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个简单的通知服务,该服务将用于向浏览网站的用户发送消息.通知不必实时发送,但如果它们发生的频率高于每 5 分钟一次,则可能会提供更好的用户体验.传入和传出客户端的数据不是很大,检索数据是一种直接的数据库查询.

I am working on a simple notification service that will be used to deliver messages to the users surfing a website. The notifications do not have to be sent in real time but it might be a better user experience if they happened more frequently than say every 5 minutes. The data being sent to and from the client is not very large and it is a straight forward database query to retrieve the data.

在阅读有关该主题的其他对话时,AJAX 推送似乎会导致更高的服务器负载.因为我可以容忍更长的服务器延迟,所以让服务器推送通知或简单地轮询是值得的.

In reading other conversations on the topic it would appear that an AJAX push can result in higher server loads. Since I can tolerate longer server delays is it worth while to have the server push notifications or to simply poll.

实现推送场景并不难,所以我想我会看看这里的意见.

It is not much harder to implement the push scenario and so I thought I would see what the opinion was here.

感谢您的帮助.

我研究了一个简单的 AJAX 推送,并基于这个 文章.初始版本的客户端负载相当低,约为 5k,并且预计会在相当长的一段时间内保持这种状态.

I have looked into a simple AJAX Push and implemented a simple demo based on this article by Mike Purvis. The client load is fairly low at around 5k for the initial version and expected to stay that way for quite some time.

感谢大家的回复.我已决定采用轮询解决方案,但将其全部封装在一个实用程序库中,以便他们以后想要更改时更容易.

Thank you everyone for your responses. I have decided to go with the polling solution but to wrap it all within a utility library so that if they want to change it later it is easier.

推荐答案

因为使用推送需要在您的服务器和每个客户端之间维护一个开放的 HTTP 连接,所以我也会去轮询 - 不仅如此消耗大量服务器资源,但正如 matt b 提到的那样,实现起来也将更加棘手.

Because using a push requires an open HTTP connection to be maintained between your server and each client, I'd go for poll as well - not only is that going to consume a lot of server resources but it's also going to be significantly more tricky to implement as matt b mentioned.

我在轮询方面的经验是,如果您在足够繁忙的站点上有足够频繁的轮询间隔,您的网络服务器日志可能会很快被轮询请求淹没.

My experience with polling is that if you have a frequent enough polling interval on a busy enough site your web server logs can get flooded with poll requests real quickly.

编辑(2017 年):我想说您现在的选择是在 websockets 和长轮询之间(在另一个答案中提到).根据问题提到不需要实时接收通知的方式,长轮询可能是正确的选择,不频繁的轮询期很容易实现,并且不应该对您的服务器造成很大负担.Websockets 很酷,是当今许多应用程序的绝佳选择,但听起来在这种情况下可能有点过头了.

Edit (2017): I'd say your choices are now are between websockets and long polling (mentioned in another answer). Sounds like long polling might be the right choice based on the way the question mentions that the notifications don't need to be received in real time, an infrequent polling period would be pretty easy to implement and shouldn't be very taxing on your server. Websockets are cool and a great choice for many applications these days, sounds like that might be overkill in this case though.

这篇关于客户端通知,我应该使用 AJAX 推送还是轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆