Php – AJAX or web sockets for client-server communication

angularjsPHPwebsockets

I recently took a course on AngularJS, and quite frankly I loved the concepts of a website that fetches json to handle subsequent requests, however I still feel like Javascript is far too slow for what I want to do on a server.

I have recently been hired to build an online auction website. I built two mockups, one with AngularJS and one in PHP. The PHP website is much faster on initial load and handling large requests, but the AngularJS-powered site is much more responsive on simultaneous requests, and writing JavaScript and HTML frontend is much easier, so I got to thinking that I might just make a counterpart to AngularJS using PHP, JS, and HTML, giving me the best of both worlds. As I go forward in implementing the design, I started to question if AJAX was the right tool for the job. It seems like I could use a simple node.js websocket server as an intermediary to between the client-side JS and PHP, passing commands from the client over websockets to the server, then passing the data back.

Would you consider this premature optimization, and if not, would removing the HTTP overhead really make much difference at all in terms of bandwidth and request size?

Best Answer

If you want typical request-response client-server communication use ajax.

If you want bidireccional communication and for example sending updates from the server to the clients use web sockets.

"It seems like I could use a simple node.js websocket server as an intermediary to between the client-side JS and PHP, passing commands from the client over websockets to the server, then passing the data back."

I don't see the point in doing something like that. If you want to use websocket use node for the server side of you application, why do you want to use node.js only as a "websocket proxy" and use php for server side operations?. What is the justification for this complexity?

Related Topic