Real time with PHP

Asked

Viewed 2,315 times

14

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

I’ve seen about long-polling however 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 understand how to use, and would like to know also your support to browsers. Searching, I found the Ratchet, but what would it be? A "framework" to assist in the use of WebSockets? Another option I’m aware of is the famous Node.js but could use it separately only for the notifications/chat part? If yes, how would this "integration/mix" between PHP and Node.js?

Anyway, even without any experience with such matters (WebSockets/Node.js) would like to use WebSockets not to have to involve Node.js and continue using current project technologies.

  • 1

    Maybe this tutorial can help you: http://www.davidchc.com.br/video-aula/php/criando-um-chat-utilizando-jquery-e-php/

  • Unfortunately this tutorial does not serve, since this chat is not in real time, jQuery just simulates it in a certain way. Such a way that I don’t exactly want to do.

1 answer

9


Websocket is a bidirectional communication protocol compatible with all current browsers. It is ideal for persistent application-server communications, allowing notifications to be sent/received in real time.

It is possible to develop a solution using PHP. The recommended one would even resort to some framework like the Ratchet, because implementing a solution with Websocket from scratch is a very complex task.

Note that competing connections in Apache are much lower to Node.js competing connections. This can be mitigated in many ways, Nginx typically has more competing connections and may be more suitable 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 are already a front-end developer, because the language is also Javascript. The difference is that with you will use Javascript on the server side.

If you choose Node.js, be sure to check out the library socket.. It works on all browsers, even the old ones that don’t even support Websocket (<= IE 9). This is due to an ingenious fallbacks mechanism: he tries to use WebSocket, if unavailable, part of the Flash Socket, next AJAX, long-polling, AJAX multipart streaming, IFrame, and finally JSONP polling. All six of these mechanisms work with the same code you write (so it pays to use a library instead of implementing Websocket at hand).

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

Remember that you don’t accurate of Node.js and introducing another platform in the structure of your application adds a higher level of complexity (monolithic architecture x microservices). Ratchet is perfectly fine comparable to socket.io in the performance benchmark. But since Node.js is growing and PHP is disappearing, it can be a good opportunity to implement small features in Node.js to learn this technology.

  • So, @rodorgas, I already have a slight knowledge about Node.js and I have a book that I am following to deepen more... It turns out that my own doubt was about the "mixture" of languages and it still leaves me confused. How would I make this "integration" of Node.js with PHP? I am totally lost and need to develop this system...

  • The ideal would be to totally rewrite my application in Node.js, but since I don’t have so much knowledge or time available I can’t do it. If there is a return then yes I will reprogram totally in Node.js. For now I need a quick and effective solution for notifications/chat...

  • 1

    @Igor Did you take a look at the chat demo? It’s not necessary to rewrite the application in Ode, because it is good only for this type of specific thing. If you follow the basic tutorial, will better understand the process. PHP is not integrated, Node runs on a separate server on a separate port. It’s all well segmented so that one is practically independent of the other.

  • I did, I even developed a few things for tests a while back. But, for example, how could I integrate a "general chat" like the example in my system that is in PHP? How I would integrate this chat into my system?

  • 1

    @Igor What aspects of integration do you refer to? " Integrate" is as generic as "program", the question sounds as airtight as "how will I program?" I will try to answer: it creates an interface that your PHP is probably generating. The output of this PHP must be an HTML such that it contains a JS that connects you, through the socket.io, in the Node.js that is running on another port on your server. Regarding the details of "how" this will take place, I have several suggestions, but in fact depends a lot on your project.

  • 1

    I got it, @rodorgas... At the moment I’ll take a makeshift with jQuery and PHP even, if you give the least significant return I will Node.js! Thanks!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.