简体   繁体   English

使用 PHP/JavaScript 从服务器向用户推送通知

[英]Push notifications from server to user with PHP/JavaScript

Short: Is there a way to push data from the server to a logged in user session without that user needing to make an HTTP request?简短:有没有办法将数据从服务器推送到登录的用户会话,而该用户不需要发出 HTTP 请求?

Long(er): I'm building an HTML5 application, on an Apache/PHP/MySQL server, that could be served to users on mobile devices (or perhaps via a PhoneGap application). Long(er):我正在 Apache/PHP/MySQL 服务器上构建一个 HTML5 应用程序,它可以提供给移动设备上的用户(或者可能通过 PhoneGap 应用程序)。

I would like to let the user know when there is new information available from the application, without racking up excessive data charges by forcing that user to execute a call to the server, or requiring AJAX calls (yes I know it could check for a single byte cookie, but I'd rather make it zero).我想让用户知道应用程序何时有可用的新信息,而不会通过强制该用户执行对服务器的调用或要求 AJAX 调用来增加过多的数据费用(是的,我知道它可以检查单个字节 cookie,但我宁愿让它为零)。

A good example of this would be sending chat messages between two users on submit from one user to the other.一个很好的例子是在从一个用户向另一个用户提交时在两个用户之间发送聊天消息。

In modern browsers, Websockets or in older browsers, " long polling ".在现代浏览器、 Websockets或旧浏览器中,“长轮询”。 There are also libraries that can abstract some of this (like comet ).还有一些库可以抽象出其中的一些(如Comet )。

A very common library in use now is socket.io which automatically connects with the best technology available, using webSockets if available and falling back to other technologies like long polling if not available.现在使用的一个非常常见的库是socket.io ,它自动连接可用的最佳技术,如果可用,则使用 webSockets,如果不可用,则回退到其他技术,如长轮询。

Some other references:其他一些参考:

How do I implement basic "Long Polling"? 如何实现基本的“长轮询”?

COMET (server push to client) on iPhone iPhone 上的 COMET(服务器推送到客户端)

More on Long Polling更多关于长轮询

Is there a difference between long-polling and using Comet 长轮询和使用 Comet 有区别吗

Pushing the web with websockets 使用 websockets 推送网络

Oh HTML5, Push Data to my Mobile 哦 HTML5,将数据推送到我的手机

I recommend Socket.io我推荐Socket.io

It leverages它利用

WebSocket, FlashSocket, HTML file(for IE), XHR long-polling, JSONP-polling WebSocket、FlashSocket、HTML 文件(用于 IE)、XHR 长轮询、JSONP 轮询

It is with Node.js which means it is on non-blocking nature.它与Node.js 一起使用,这意味着它具有非阻塞性质。

In realtime feature, concurrency is a big issue在实时特性中,并发是一个大问题

so the non-blocking nature of Node.js and Socket.io would help you所以 Node.js 和 Socket.io 的非阻塞特性会帮助你

ScaleDrone允许您使用PHP API 客户端从 PHP 推送到 JavaScript。

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

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