Real time with PHP

14

I am developing a system and soon I will have to start the part of interactions in real time (for notifications and chat). I am using jQuery , PHP and MySQL so far and I intend to continue with these technologies.

I have already seen about long-polling but I read that it is not a good practice, since, several requests would be made to the server and depending on the number of accesses it could overload. I also read about WebSockets but I still can not figure out how to use it, and I would also like to know your browser support. Researching, I found Ratchet , but what would it be? A "framework" to assist in the use of WebSockets ? Another option that I came to know is the famous Node.js , but how could I use it separately for the notifications / chat part only? If yes, how would this "integration / blending" between PHP and Node.js ?

However, even without having any experience with such subjects ( WebSockets / Node.js ) I would like to use WebSockets so I do not have to involve Node.js and continue using current project technologies. >     

asked by anonymous 15.05.2015 / 22:02

1 answer

9

WebSocket is a two-way communication protocol compatible with all current browsers . It is ideal for persistent communication between application-server, which allows notifications to be sent / received in real time.

It is possible to develop a solution using PHP. It is recommended to use some framework such as Ratchet , because implementing a solution with WebSocket from scratch is a good job complex.

Note that competing connections in Apache are much lower to competing connections of node.js. This can be mitigated in several ways, nginx typically has more concurrent connections and may be more suited for this.

node.js

Another more popular alternative to Ratchet / PHP for this type of task would be node.js, used in Microsoft Office, Yammer, Zendesk, Trello, hackathons and small startups. And yes, it is possible, normal and very common to use PHP / Python / Ruby / ASP and node together. Getting started with node.js is nothing new if you're already a front-end developer, because the language is JavaScript as well. The difference is that you will use JavaScript on the server side.

If you choose node.js, be sure to take a look at the socket.io library. Works on all browsers, even the old ones that do not even support WebSocket (< = IE 9). This is due to an ingenious fallback mechanism: it tries to use WebSocket , if not available, part to Flash Socket , then AJAX , long-polling , AJAX multipart streaming , IFrame , and finally JSONP polling . All of these six engines work with the same code you write (so it pays to use a library instead of deploying WebSocket).

This chat demo is used in the documentation. The application on the server has only 79 lines of code, quite simple.

Remember that you do not need node.js and introduce another platform in your application structure adds a higher level of complexity (monolithic architecture x microservices ). Ratchet is perfectly comparable to socket.io in the performance benchmark . But since node.js is growing and PHP is disappearing, it might be a good opportunity to implement small features in node.js to learn this technology.

    
19.05.2015 / 01:34