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.
Maybe this tutorial can help you: http://www.davidchc.com.br/video-aula/php/criando-um-chat-utilizando-jquery-e-php/
– William Urbano
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.
– Igor