How does LAMP Guy easily implement WebSockets?

I have always worked with Apache, MySQL and PHP. I would like to end up with Python / Django or Ruby / Ruby on Rails, but this is another discussion. Two great things about Apache, MySQL and PHP - all three are ubiquitous, and it is very easy to run a website. Just set up the Apache virtual host, import the database into MySQL and copy the PHP files to the server. It. That is all that I have ever done, and all that I have ever known. Remember this.

Today, it is becoming increasingly important that websites can provide real-time data to users. Users expect this too due to the wildlife of Facebook and Gmail. This effect can be falsified by an Ajax poll, but it has a lot of overhead, as described here . I would like to use WebSockets. Now remember that I have always been a LAMP guy. I only ever run websites using the method that I described earlier. So, if I have, say, a CakePHP website, how can I add a WebSockets function? Do I need to install some other server or something else, or can I make it work with Apache? Will it require Apache 2.4? Please explain this process to me, bearing in mind that I only know about LAMP. Thanks!

+7
source share
1 answer

One of the key things to keep in mind is that the real-time web server server must be โ€œlongโ€ so that it can push information to clients. In the classic LAMP setup, Apache runs a PHP interpreter for each request. Between requests, the PHP interpreter does not work, and the only state of the protocol between requests is a session.

One of the nice features of the LAMP method is that memory management is easy. You simply implicitly allocate the memory you need, and it is automatically restored after the request is completed, and the PHP process ends. As soon as you want the server to continue to work, you need to consider memory management. In some languages, like C ++, you control the allocation and release explicitly. In other languages, such as Java or Javascript, you have garbage collection. In PHP, you drop everything and start with a fresh slate for every request.

I think it will not be easy for you to make servers with long servers with something like Cake or any other classical PHP framework. This framework works mainly by using an HTTP request and turning it into an HTTP response.

My advice is that you should learn something like Node.JS and SocketIO. If you know Javascript or do not mind learning, these technologies will allow you to easily implement real-time servers and clients. If necessary, you can run a reverse proxy, such as nginx, so that your existing LAMP stack receives some requests, and one or more NodeJS servers can receive some.

This answer came out a bit fluffy, but I hope this helps a little .. :-)

+4
source

All Articles