Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax vs. Web sockets vs. Web Workers

Tags:

html

ajax

web

What is the difference between all three? They seem to do the exact same thing. Why and when would you choose to use one method over the other?

like image 451
Miriam P. Raphael Avatar asked Sep 28 '12 18:09

Miriam P. Raphael


People also ask

Which is better Ajax or WebSocket?

However, Websocket push is far superior to AJAX (no need to re-authenticate or resend headers, no need for "no data" roundtrips, etc').

What is the difference between WebSocket and Ajax?

Ajax uses the HTTP Protocol and can send requests using POST/GET methods from Client to Server. WebSocket is itself a protocol to communicate between Client and Server, distinct from HTTP. In Ajax when you send a request , server sends response for that request and connection ends.

What is the difference between a socket and a web socket?

WebSockets typically run from browsers connecting to Application Server over a protocol similar to HTTP that runs over TCP/IP. So they are primarily for Web Applications that require a permanent connection to its server. On the other hand, plain sockets are more powerful and generic.

Are web workers and Service Workers same?

Unlike web workers, service workers allow you to intercept network requests (via the fetch event) and to listen for Push API events in the background (via the push event). A page can spawn multiple web workers, but a single service worker controls all the active tabs under the scope it was registered with.


4 Answers

AJAX and websockets do similar tasks — they both establish a communication channel to a server. Web workers have nothing to do with either of them, they are just separate threads of JS execution.

AJAX is more mature than websockets — it has been around much longer and has a much wider browser support. AJAX is request-oriented — you make a request to the server, the server responds, and the connection is closed. Websockets on the other hand establish a persistent connection to the server, over which you exchange multiple messages in both directions.

Webworkers are useful if you want to perform a processor intensive task without blocking the browser interface.

like image 84
lanzz Avatar answered Oct 04 '22 01:10

lanzz


They are not same. But one can use them together to build advanced application.

Ajax: As abbrevation States is asynchronous javascript and xml.. is used to load the content dynamically from the server upon called.

Websockets : Websockets is the feature defined in HTML5 . As wikipedia states "WebSocket is a protocol providing full-duplex communications channels over a single TCP connection." so this is mainly used for real time communication such as video call, live chat etc..

WebWorkers : this feature is also defined in HTML5. This is basically used to make bring multi threading feature in Javascript. Since javascript is a single threaded programming language , it breaks or pause whenever i.e heavy calculation tasks are done using it. to overcome this breakage , Webworkers are added to javascript.

You can perform Ajax and Websockets inside Webworkers . however you cannot manipulate DOM using webworkers due to security reasons.

like image 45
Saajan Avatar answered Oct 04 '22 01:10

Saajan


They are not the same.

Ajax: It is a way of interacting with a web server asynchronously from a UI renderer

Web Sockets: An HTML5 feature using which you can interact with any Socket server extending the reach of the browser

Web Workers : Another HTML5 feature that helps you do multi-threaded programming from a web browser using Java Script

like image 29
srini.venigalla Avatar answered Oct 04 '22 02:10

srini.venigalla


  • Ajax and Websockers are siblings.
  • Webworkers are different.

AJAX

  • The best example of AJAX is Google's search bar - suggestions appear as you type, but the current webpage is not redirected or refreshed! (10 years ago this was amazing, not so much anymore). This is AJAX in action.

  • AJAX uses what's called a "request" and "response" model: you ask a question, and you receive an answer. The end. You can think of it like writing letters by hand in the old days.

  • AJAX allows webpages to talk to "servers" behind the scenes, allowing you to update a webpage without navigating away from your URL. Back in the old days of the web, if you wanted to show different content on a webpage, users would have to navigate to a different URL: not any more. This concept has been taken to the next level with single page apps and applications (like React, Vue, Elm etc.).

Websockets:

  • With websockets, your web-page talks to your server (as with Ajax), and your server responds - except you do so like you're talking on the phone. There is a "connection" between your users and your server. This "connection" is not there with AJAX: in that case, you have a simple request and a response coming back from the server.

In other words, if you wanted to stream stock market data, constantly updating it to your users: it would probably be better to use websockets, than AJAX.

Web-workers:

Use When you need intensive calculations - if you were to ask a web-page to calculate Pi to 100000 decimal places: that might take a while. The web-page might freeze, and you might lose $$.

Therefore, the internet boffins thought it was a good idea to get all the hard work to be done by "workers". If you do so, then the work could be done, without holding your web-page hostage.

like image 29
BenKoshy Avatar answered Oct 04 '22 02:10

BenKoshy