Discussion of push (push) technology in PHP (reprint)

Source: Internet
Author: User
Discussion on push (push) technology in PHP (reprint)

? ? ? (Reproduced: Http://vistaswx.com/blog/article/php-server-push).

? ? ? Find a lot of information, this information is the feeling of the clearest explanation. Copy it down for later use.?

??? With the increasing demand for instant Web applications, Server push (push) technology is emerging as a data stream core for real-time applications, such as chat, message alerting, and social networking in particular. This log attempts to explore the various implementations of the push that are appropriate for PHP and their pros and cons.

1. What is server Push

Imagine in the chat application, if the use of traditional Ajax to assume the message of the incoming, then generally through a certain time to pull the information of the way to achieve, but in fact, this way there is a lot of query is wasted. Web applications such as chat require the server to proactively inform the front-end of new messages (Push) at a specific time, rather than the front-end asking the server: "Did you get the message?" "(pull). This is why this technique is often called reverse Ajax.

Other aliases: Comet, reverse Ajax

?

2. How to implement push

In fact, the so-called push technology is not how complex, now there are 3 kinds from the big class, one is still based on Ajax, there is a framework based on the last one abandoned the traditional HTTP protocol, using Flash or HTML5 websockets technology. The next step is to explore the different ways in which these three categories are produced.

?

1) Ajax Long polling

Ajax long polling is still essentially a pull, but it is a good push implementation because of the high real-time and much less useless requests. But it only reduces the unnecessary consumption on the network.

Core:? The client initiates an AJAX request that the server will shelve (pending) or suspend until the time-out (timeout) or push is required, and the client waits for Ajax to process the data before initiating the next Ajax request.

Advantages:high compatibility, simple implementation

Cons:? For PHP This language, if you want to do real-time, then the server will have to withstand much greater pressure, because when the time is often uncertain, it is necessary for each time the PHP script shelved a while loop.
Of course, if the server refreshes every second level, that is acceptable, but in real-time degradation.

Note:The browser has a limit on the number of connections. I came to the conclusion that if there is an AJAX request on the current page waiting to return, other AJAX requests will be shelved (Chrome, Firefox has been tested). seems to be related to page markup, a canonical HTML can have multiple requests at the same time. What if the page has general AJAX requirements? The workaround is to open a framework in which the comet long polling under another domain name requires attention to cross-domain issues.

PHP implementation:? jquery+php implementation of Comet

Related:? Ajax cross-domain and JS cross-domain Solutions

?

2) Frame Long connection

Inspired by Ajax, there is a long connection under the frame.

Core:? A normal request is initiated in frame, the server holds it, and the output is executed directly when push is required.
Script, and then continue to remain connected. If you are concerned about the time-out problem can be changed to frame inquiry.

Pros:1 with the same high compatibility characteristics

Cons:? The biggest problem is that if the framework is loaded, then the browser will always show "load in", which will be weak (see the relevant reading resources at the end of this article for a workaround). The same server can hold a lot of loops ... In addition, there are no same domain connection restrictions that are not tested.

?

3) FLASH/HTML5 WebSockets

Use Flash to launch websockets, seconds to kill all the problems before.

Advantages:standardization, RealTime, Push

Cons:server needs to be able to deal with WebSockets, and if neither flash nor support HTML5?

PHP implementation:? Start Using HTML5 WebSockets Today

?

6) Using a compatible packaging layer (Socket.io)

Each of these approaches has pros and cons, so the ultimate solution is to be together! Can websockets time on the websockets, does not support the HTML5 feature degradation to flash, no flash is degraded to Ajax long polling. That's the way my rainbowfish used to be.

Advantages:High-encapsulation, writing is very easy, almost no need to care how to achieve. Real-time, ultra-low load, high concurrency.

Cons:? Actually not a disadvantage, Socket.io server-side requirements are node. js, not PHP.

personal opinion:? If you are a standalone host and can run the program, then socket.io with node. JS is a very efficient option. Why is it? Because it can also avoid the high load of PHP service side.

Rainbowfish's messaging system is implemented in this way: All clients are hung on the Nodejs server via Socket.io (note: Just hang up, no loops required because it is event-driven), and the server communicates with Nodejs if the message is to be pushed ( such as access to an address to achieve), tell it what to push the message to where, Nodejs received a push signal, the real-time transmission of data through Socket.io to the browser. This is actually a one-way road, because the NODEJS server does not have the ability to communicate with PHP, in fact, do not need, the page directly connected to PHP.

?

3. Concluding remarks

In fact, the first method (Ajax Long pull) is a good way to do it, but if you use PHP, the server load is a bit large, but it's a common problem. , and the final list of Socket.io scenarios completely avoids this problem because it belongs to a different architecture, and this combination can be implemented with almost all scripting languages.

For applications with very high real-time requirements, perhaps using PHP to implement real-time parts is not a good choice, and will face very large server loads (which can be solved by writing an extension that supports waiting events), and if it is just a message prompt, you can adjust the refresh interval on the server down to the second level. The load is acceptable. But whatever the use, it may be the best option to work with non-blocking languages.

?

4. Related Reading

How to implement COMET with PHP

Start Using HTML5 WebSockets Today

Comet (Wikipedia)

Ajax cross-domain and JS cross-domain Solutions

jquery+php implementation of Comet

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.