Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websocket advantages over AJAX for simple applications with PHP

I've used a bit of AJAX with PHP for things like submitting forms and I've recently started looking into websockets. I followed this tutorial to understand the basics. From what I gather, websockets keep the connection open whereas AJAX opens and closes a request.

My question is do websockets provide any advantage over AJAX if you're just submitting forms or simple tasks like auto_complete (which there's a jQuery plugin for anyway)? Maybe the tutorial isn't the greatest, but it seems like there's a heck of a lot more code involved to get websockets to work(at least with PHP) than just a simple AJAX call (or using jQuery which bundles it). I've read in a few places that it's a bit quicker, but if I'm working on something that isn't receiving tons of requests, will it really make a difference? Correct me if I'm wrong, but not all browsers support websockets either, right?

like image 743
user1104854 Avatar asked Dec 07 '12 14:12

user1104854


People also ask

Is WebSocket better than AJAX?

AJAX returns a single response to a single request. However, AJAX is asynchronous; thus, a web front-end can send any number of AJAX requests without having to wait for each response. WebSockets on the other hand can handle multiple bidirectional messages between the browser and the server.

Is WebSocket faster than AJAX?

To give you the “TL;DR” answer: yes, websockets are faster than Ajax, but you will still need Ajax sometimes.” My answer doesn't even take Andrea's response into account— if the actual implementation has less overhead, that's also great!

Can you use WebSockets with PHP?

You can decide to use any WebSocket-based protocol that supports PHP.

What is the purpose of WebSockets?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.


2 Answers

Websockets have two advantages.

  1. they have much less overhead, which results in a better network performance

  2. they allow the server to send data which the client hasn't explicitely requested.

The second one is the most important advantage.

In AJAX, everything the server sends must be the response to a previous request by the client, and every request can only be answered once. But in many applications, especially multi-user applications, events happen on the server and these events must be pushed to the clients immediately. There are workarounds for that in AJAX, like delaying the answer to a request until there is something to report (long-polling), but these are quite dirty. That's why there are Websockets. With a websocket connection, the server can send messages to the clients when it wants and as often as it wants, without having to wait for a request from the client.

But unfortunately WebSockets also have disadvantages:

  1. They aren't as well-supported by web development frameworks (yet!)
  2. Not all web browsers support it (but most desktop browsers already do)
  3. Many proxies and reverse-proxies can't relay websocket traffic (yet!)
like image 107
Philipp Avatar answered Sep 24 '22 08:09

Philipp


Actually, AJAX and websockets are two different categories. AJAX is a concept, a technique. With AJAX you can perform (as the acronym stands for) asynchronous requests, so the browser doesn't need to reload/refresh the whole page. This is good for different things, e.g. checking form input. Websockets are a protocol, technically quite the same as http, unless the connection will not be closed after transmition. This is good for things, where the webserver may need to contact the client (http can't do that), like a push service fore example (chat or mail client where you want to update the user interface even when the user does not refresh the page, or games). And it kills the http overhead as the whole thing has only to be done once in the beginning.

So, their for different purposes, even if they overlap. For your auto-completion I think it won't make a real difference in performance. And it even is a action/reaction thing, so the user types or submits (whatever) what can cause a request and the server responds.

like image 42
Cravid Avatar answered Sep 25 '22 08:09

Cravid