Research on Push technology in PHP

Source: Internet
Author: User
Discussion on Push technology in PHP (reproduced )??? (Reprinted :? Vistaswx. comblogarticlephp-server-push ).??? I found a lot of information, which is the clearest explanation. Copy it for future use .???? With the increasing demand for instant Web applications, the ServerPush technology is exploring the Push technology in chat, message reminders, especially in PHP (reprinted)

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

? ? ? I found a lot of information, which is the clearest explanation. Copy it for future use .?

? ? ? With the increasing demand for real-time Web applications, Server Push (Push) technology has become the core of real-time application data streams in chat, message reminders, especially social networks. This log attempts to explore various Push implementation methods suitable for PHP and their advantages and disadvantages.

1. what is Server Push?

Imagine that in a chat application, if the traditional ajax is used to transmit messages, it is generally implemented by pulling information once every certain time, however, this method is a waste of queries. For Web applications such as chat, the server needs to actively inform the front-end of new messages (Push) at a specific time, instead of asking the server at the front-end every moment: "Is there a message ?" (Pull ). This is why this technology is often called reverse ajax.

Other aliases: Comet, reverse Ajax

?

2. how to implement Push

In fact, the so-called push technology is not so complex. currently there are three types of push technology in the main category, one is still based on ajax, and the other is based on the framework, the last one abandoned the traditional HTTP protocol and used Flash or HTML5 WebSockets technology. Next, we will discuss the different methods of these three categories.

?

1) Ajax long polling

Ajax long polling is still a kind of pull in essence, but it has a high real-time performance and a lot of useless requests. it is a good Push implementation solution. However, it only reduces unnecessary consumption on the network.

Core:? When the client initiates an ajax request, the server suspends or suspends the request until the time-out (timeout) expires or the push is required. the client waits for ajax to return post-processing data, then initiate the next ajax request.

Advantages:? High compatibility and easy implementation

Disadvantages:? For php, if you want to implement real-time processing, the server must be put under a lot of pressure, because it is always uncertain when to put it on hold, this requires the php script to execute a while loop every time it is shelved.
Of course, if the server refresh every second, it is acceptable, but the real-time performance degrades.

Note:? The browser has a limit on the number of connections.I come to the conclusion that if an ajax request on the current page is waiting for return, other ajax requests will be put on hold (Chrome, Firefox tested ).It seems to be related to the page tag. a standard HTML can have multiple requests at the same time.What if there is a general ajax requirement on the page? The solution is to open a framework to enable Comet long polling under another domain name. you need to pay attention to cross-origin issues.

PHP implementation:? Implement comet with Jquery + php

Related:? Cross-Origin Ajax and js cross-origin solutions

?

2) Frame persistent connection

Inspired by ajax, there is a persistent connection under the framework.

Core:? Frame initiates a common request, and the server puts it on hold; when push is required, the output is executed directly.
Script, and then continue to maintain the connection. If you are worried about timeout, you can change it to a framework discussion.

Advantages:? High compatibility with 1

Disadvantages:? The biggest problem is that if the framework is loading, the browser will always display "loading", which will be weak (for the solution, refer to the related reading resources at the end of this article ). The server must also be able to hold a large number of loops ...... In addition, whether the same domain connection restriction exists is not tested.

?

3) Flash/HTML5 WebSockets

Use flash to launch WebSockets and kill all the problems above.

Advantages:? Standardization, RealTime, Push

Disadvantages:? The server must be able to cope with WebSockets. what should I do if there is neither Flash nor HTML5?

PHP implementation :?Start Using HTML5 WebSockets Today

?

6) use the compatible encapsulation layer (socket. io)

Each of the above methods has advantages and disadvantages, so the ultimate solution is to combine! When WebSockets is available, WebSockets degrades to Flash if HTML5 features are not supported. if Flash is not supported, it degrades to Ajax long polling. This is also the way I use Rainbowfish.

Advantages:? Highly encapsulated and easy to write, with almost no need to care about how to implement it. Real-time, ultra-low load, and high concurrency.

Disadvantages:? In fact, it is not a disadvantage. the socket. io server must be node. js, not php.

Personal opinion:? If you are an independent host and can run programs, it is very efficient to use socket. io with node. js. Why? Because it can also avoid high load on the php server.

The message system of Rainbowfish is implemented in this way: all clients use socket. io is mounted on the nodejs server (note: it is only hang and does not need any loops because it is event-driven); messages need to be pushed, the server communicates with nodejs (such as accessing an address) and tells it where to push the message. after nodejs receives the push signal, it uses socket. io transmits data to the browser in real time. This is actually a one-way approach, because the Node. js server does not have the ability to communicate with php, and actually does not need to. you can directly connect to php on the webpage.

?

3. Conclusion

In fact, the first method (Ajax Long Pull) is a good method, but if php is used to complete the process, the server load is a bit large, but this is actually a common problem; and the last socket. i/O solution completely avoids this problem because it belongs to another architecture, and this combination can also be used with almost all scripting languages to implement push.

For applications with high real-time requirements, using php to implement real-time data is not a good choice, there will be a lot of server load (this problem can be solved by writing extended events that support waiting); if it is just a message prompt, you can adjust the interval of refreshing on the server to the second level, and the load is acceptable.However, whatever the purpose, it may be the best choice to work with non-blocking languages.

?

4. reading

How to implement COMET with PHP

Start Using HTML5 WebSockets Today

Comet (Wikipedia)

Cross-Origin Ajax and js cross-origin solutions

Implement comet with Jquery + php

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.