Web应用程序的推送通知 [英] push notification for a web application

查看:58
本文介绍了Web应用程序的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Web应用程序中具有推送通知的有效方法是什么?

我有一个Web应用程序,我希望通过推送通知为每个特定用户动态刷新它,而不是定期从服务器提取数据.

解决方案

最好的解决方案不存在,但是您可以通过一些方法来完成.

Web套接字(IMO,我推荐这种方法)

第一个也是最有效的方法是在HTML5页面中使用Web套接字.您可以使用PHP中的库来完成此操作,也可以使用带有Socket.io或类似程序的Node.js服务器来处理站点的前端(我在在线产品中经常看到这一点).

注意:使用Web套接字时,客户端和服务器将互相发送TTL(生存时间)消息,这是一条很小的消息,带有小标题,只是为了保持连接的打开和活动./p>

短轮询

第二个是短轮询,这里您每隔X秒或每分钟向服务器发出一个ajax请求(取决于您要刷新数据的频率).注意:这是效率最低的,因为您正在向服务器打开大量请求,只是为了检查是否有新数据

长轮询

第三个是长轮询,这类似于短轮询,但是您将单个连接打开了较长的时间,并在有数据要发送到客户端时从服务器获得响应.这样比较省钱,但是您仍然可以保持客户端和服务器之间的永久连接打开.

What is an efficient way to have push notification in a web application?

I have a web application and I want it to be dynamically refreshed for every specific user by push notification instead of regularly pulling the data from the server.

解决方案

The best one doesn't exist, but you have some options to get this done.

Web sockets (IMO, I recommend this approach)

The first one and the most efficient is to use web sockets in an HTML5 page. You could use a library in PHP to get this done, or you could use a Node.js server with Socket.io or similar to handle the frontend of your site (I see this a lot in online products).

Note: When using web sockets the client and the server will be sending each other a TTL (time-to-live) message, this is a really really small message with small headers just to keep the connection open and alive.

Short-polling

The second one is short polling, here you make an ajax request to the server every X seconds or minutes (depends on the frequency that you want to refresh the data). NOTE: this is the least efficient, because you're opening a lot of request to server just to check if there's new data

Long-polling

The third one is long polling, this one is like short polling but instead you keep a single connection opened for a longer period of time and get a response from the server when there's data to be sent to the client. This is less consuming, but you still keep on permanent connection open between the client and the server.

这篇关于Web应用程序的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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