开发者

Opposite of AJAX

开发者 https://www.devze.com 2023-02-10 09:58 出处:网络
Is there is a way to implement server triggered communication that updates a page (not the entire page) in PHP?

Is there is a way to implement server triggered communication that updates a page (not the entire page) in PHP?

In other words is there a way to implement a "modified" AJAX where 开发者_JAVA百科the server initiates communication?


You are probably thinking about push technologies, like COMET or long polling. See http://en.wikipedia.org/wiki/Comet_(programming) and http://en.wikipedia.org/wiki/Long_polling#Long_polling


You can not open a connection from the server to the client. You need to do some sort of polling from the client to the server. You can look into COMET or just poll the server after set periods of time to check from new content.


Yes, you can use long polling HTTP, or a web socket in some of the newer browsers (Chrome).


You need to look at something called a variety of names, such as Comet, AJAX Push, Server Push, etc.

http://en.wikipedia.org/wiki/Comet_(programming))

It keeps the HTTP connection open to receive communication from the server at some point in time.


You are talking about "reverse ajax" aka sometimes "comet". Reverse ajax is not really part of the http spec, it's a bit of a hack. Essentially it involves leaving an HTTP request open, with the server sending back responses whenever. It's not for the feint-hearted though and to be honest for 99% of applications probably is not worth the stress. You'll need a comet server to handle and manage all the open connections. Seriously consider auto-refresh regular ajax calls or something.


What you need is SSE (Server-Sent Events). The only drawback is that it's not yet supported by any of IEs, so you'll probably need to implement some feature-detection too (with, eg, modernizr.js) and use either long-polling or SSE depending on whether your browser support SSE (or just use a SSE polyfill, there are plenty thankfully).
Why would you probably want to implement both techniques instead of going with well-supported long-polling? Server-Sent Events vs Polling.

Here is a tutorial on PHP implementation: http://www.w3schools.com/html/html5_serversentevents.asp

And a good explanation of what SSE gives you:

HTML5 introduced an API to work with Server-Sent Events. The main idea behind SSE is simple: the web page subscribes to an event source on the web server that streams updates. The web page does not have to constantly poll the server to check for updates (as we’ve done with AJAX polling) – they come automatically. Please note that the script on the client side can only listen to the updates, it cannot publish anything (compare this to Web Sockets where the client can both subscribe and publish). Therefore, all publishing functionality is performed by the server.

0

精彩评论

暂无评论...
验证码 换一张
取 消