Websocket advantages over AJAX for simple applications with PHP

I used a bit of AJAX with PHP for things like submitting forms, and I recently started learning websockets. I understood this tutorial to understand the basics. From what I collect, websockets keep the connection open, while AJAX opens and closes the request.

My question is, do websockets provide any advantage over AJAX if you just submit forms or simple tasks like auto_complete (for which there is a jQuery plugin anyway)? The tutorial may not be the largest, but it seems that it involves much more code for working websockets (at least with PHP) than just a simple AJAX call (or using jQuery that binds it). I read in several places that this is a little faster, but if I work on not receiving tons of requests, will it really matter? Correct me if I am wrong, but not all browsers support web ports, right?

+6
source share
3 answers

Web sites have two advantages.

  • they have much less overhead, which leads to better network performance

  • they allow the server to send data that the client did not explicitly request.

The second is the most important advantage.

In AJAX, all server forwarding should be a response to a previous client request, and each request can only be received once. But in many applications, especially multi-user applications, events occur on the server, and these events must be immediately redirected to clients. There are workarounds in AJAX, for example, deferring a response to a request until a message appears (long poll), but they are pretty dirty. This is why there are Websockets. By connecting to the web server, the server can send messages to clients whenever it wants, and as often as it wants, without having to wait for a request from the client.

But, unfortunately, WebSockets also has disadvantages:

  • They are not well supported by web development infrastructures (for now!)
  • Not all web browsers support it ( but most desktop browsers already do )
  • Many proxies and reverse proxies cannot transmit websocket traffic (yet!)
+18
source

Actually, AJAX and websockets are two different categories. AJAX is a concept, a technique. With AJAX, you can perform (as indicated in the abbreviation) asynchronous requests, so the browser does not need to reload / refresh the entire page. This is good for different things, for example. validation form input. Websockets is a protocol technically the same as http if the connection is not closed after the transfer. This is good for things where the web server may need to contact the client (http cannot do this), for example, an example of a forwarding service (chat or email client, where you want to update the user interface, even if the user does not refresh the page or games). And it kills the http overhead, since all this needs to be done only once at the beginning.

So, they are for different purposes, even if they overlap. For your autocompletion, I think this will not affect performance. And this is even an action / reaction, so the user enters or transmits (independently) what can cause the request, and the server responds.

+9
source

Websockets is a powerful technology and, of course, can satisfy the limited use case that you mentioned, but compatibility issues with older browsers and network intermediaries may occur. In fact, some people even recommend having an HTTP backup in case websites are not supported.

If you do not have requirements that require web connections, such as two-way two-way communications, for example, you might be better off using existing AJAX-based solutions.

If you have push notification requirements in your user interface, Websockets might be a good idea, but if you are literally looking for form submission and autocompletion, then these problems have already been solved with Ajax.

+1
source

All Articles