Sitemap
VirtouStack

We design custom apps for different business spheres and clients. Our team will take into account all your wishes and needs to make the best app for you.

(When || Why || Where) Node.js?

4 min readMay 7, 2022

--

Introduction

JavaScript’s rising popularity has brought with it a lot of changes, and the face of web development today is dramatically different. The things that we can do on the web nowadays with JavaScript running on the server, as well as in the browser, were hard to imagine just several years ago, or were encapsulated within sandboxed environments like Flash or Java Applets.

After over 20 years of stateless-web based on the stateless request-response paradigm, we finally have web applications with real-time, two-way connections. In one sentence: Node.js shines in real-time web applications employing push technology over websockets. This is in stark contrast to the typical web response paradigm, where the client always initiates communication. Additionally, it’s all based on the open web stack (HTML, CSS and JS) running over the standard port 80.

When to use Node.js

Node really shines is in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability. You definitely don’t want to use Node.js for CPU-intensive operations; in fact, using it for heavy computation will annul nearly all of its advantages.

Press enter or click to view image in full size
Traditional vs Node.js

Why use Node.js?

If your use case does not contain CPU intensive operations nor access any blocking resources, you can exploit the benefits of Node.js and enjoy fast and scalable network applications.

Examples of where Node.js Should Be Used

Chat

The chat application is really the sweet-spot example for Node.js: it’s a lightweight, high traffic, data-intensive (but low processing/computation) application that runs across distributed devices.

On the server-side, we have a simple Express.js application which implements two things:

  1. A GET / request handler which serves the webpage containing both a message board and a ‘Send’ button to initialize new message input, and
  2. A websockets server that listens for new messages emitted by websocket clients.

On the client-side, we have an HTML page with a couple of handlers set up, one for the ‘Send’ button click event, which picks up the input message and sends it down the websocket, and another that listens for new incoming messages on the websockets client (i.e., messages sent by other users, which the server now wants the client to display).

Get Vinay Bojja’s stories in your inbox

Join Medium for free to get updates from this writer.

When one of the clients posts a message, here’s what happens:

  1. Browser catches the ‘Send’ button click through a JavaScript handler, picks up the value from the input field (i.e., the message text), and emits a websocket message using the websocket client connected to our server (initialized on web page initialization).
  2. Server-side component of the websocket connection receives the message and forwards it to all other connected clients using the broadcast method.
  3. All clients receive the new message as a push message via a websockets client-side component running within the web page. They then pick up the message content and update the web page in-place by appending the new message to the board.
Chat

DATA STREAMING

In more traditional web platforms, HTTP requests and responses are treated like isolated event; in fact, they’re actually streams. This observation can be utilized in Node.js to build some cool features. For example, it’s possible to process files while they’re still being uploaded, as the data comes in through a stream and we can process it in an online fashion. This could be done for real-time audio or video encoding.

Where Node.js Shouldn’t Be Used

Heavy server-side computation/processing

When it comes to heavy computation, Node.js is not the best platform around. No, you definitely don’t want to build a Fibonacci computation server in Node.js. In general, any CPU intensive operation annuls all the throughput benefits Node offers with its event-driven, non-blocking I/O model because any incoming requests will be blocked while the thread is occupied with your number-crunching — assuming you’re trying to run your computations in the same Node instance you’re responding to requests with.

Conclusion

We’ve discussed Node.js from theory to practice, beginning with its goals and ambitions, and ending with its sweet spots and pitfalls.

“In Node, blocking operations are the root of all evil — 99% of Node misuses come as a direct consequence.”

Reference :https://www.toptal.com/nodejs/why-the-hell-would-i-use-node-js#:~:text=PROXY-,Node.,data%20from%20multiple%20source%20points.

--

--

Published in VirtouStack

We design custom apps for different business spheres and clients. Our team will take into account all your wishes and needs to make the best app for you.