Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use node.js and when to use ajax? [closed]

I had a website , which was coded entirely in php and basic jquery ajax . However after i learned about node.js/socket.io i recoded almost all of the real time stuff that involved ajax using nodejs and socket.io . I am a bit confused as to whether i did the right thing . So my question is when is ajax a more optimal solution than using node/socket ?

like image 223
AnuragD Avatar asked May 28 '13 15:05

AnuragD


People also ask

What is the difference between NodeJS and AJAX?

NodeJs is an open-source framework based on JavaScript v8 engine. AJAX is a web development technique for making asynchronous calls to the server. jQuery is a JavaScript library for designing and make some web development tasks easy. It makes it possible to run javascript outside of the browser.

When should we not use NodeJS?

Applications with heavy computing server-side. Since Node. js uses only one CPU core, heavy computations on the server will block all other requests. In this case, the event-driven non-blocking I/O model which is the strongest side of Node. js will become useless, and the application performance will suffer.

Does node js need AJAX?

The short answer: Ajax does not require jQuery nor Node. js. In practice, Ajax is a technology for asynchronous operations utilized by Javascript send data to and retrieve from a server asynchronously(1).

What are the use cases for node JS and when it should be avoided?

NodeJS has become the number one choice among developers as well as companies for building modern web applications. However, you should not use NodeJS if you're developing a CPU-intensive web application as it may cause unresponsiveness due to its single-threaded nature.


2 Answers

Firstly, you should learn about the difference between the two, the answer will become clear.

AJAX are used for simple asynchronous requests. They don't require setting up a Node server and are supported on nearly every browser. They are used when you need to retrieve a piece of information from the server. They do however have the overhead of being sent over HTTP and therefore they need to be a proper HTTP request/response (adds weight).

WebSockets (available in a Node.js/socket.io setup) are used when you access some data frequently or you need to have a live, persistent connection with the server. You can establish a socket connection and send packets from the server to the client. This is lightweight compared to an AJAX solution, however Websockets are not supported by older browsers, and you need to setup a server that will handle such requests.

Socket.IO in particular uses a collection of different techniques so that you can get better browser support: long polling, multipart streaming etc.. This allows you to have that "instant" feedback from the server, however it still in most cases uses HTTP as the protocol. It will however use WebSockets if they are available (i.e. supported by the browser).

Actually in some cases Node.js/Socket.io will use AJAX.

If you're not working on an online game or if you don't need to update the state of your application frequently, I would suggest using AJAX instead of a Node.js/socket.io setup.

like image 117
MMM Avatar answered Oct 01 '22 14:10

MMM


Nodejs is great for applications where you need to keep a persistant connection open between the client and the server. Basically if you want to send realtime data (chat client, game, etc.) between the client and server nodejs is a great option.

With nodejs this kind of persistent connection won't block other requests. Many other langauges like php (where each connection starts a new server process) struggle in such situations and you will likely end up with huge CPU load.

Socket.io and node.js together is an even better option if realtime data with the lowest possible latency is the goal. It will only fall back to long-polling when other technologies aren't supported, its preference is to use web sockets.

Of course both apache (with php) and nodejs can do AJAX, the question should really be whether that's the right way for you to transfer data, we need to know more about what you're trying to achieve!

like image 39
nick Avatar answered Oct 01 '22 13:10

nick