Friday, May 7, 2021
More

    Why use Node.js ?

    Must Read

    Programmerhttp://www.improgrammer.net
    We started this site to inspire young minds to motivate and encourage them towards Programming Language. In this site you will get programming tutorials, tech, programming facts, programming fun and programming blogs.

    In This article, we discuss about Node.js, where Node.js used with example and How do Node.js works?

    Introduction

    JavaScript’s growing acceptance has brought with it a lot of development, and the face of web development today is seriously different. This stuff we can do on the web today with JavaScript working on the server, as well as in the browser, were hard to plan just several years ago or were hide within sandboxed environments like Flash or Java Applets.

    Up to now go into Node.js, you might need to read up on the profit of using JavaScript across the stack which consolidates the language and data format (JSON), admitting you to optimally estate developer effects. As this is more an advantage of JavaScript than Node.js categorically, we won’t discuss it much here. But it’s a key advantage to assimilate Node in your stack.

    As Wikipedia states: “Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” Beyond that, it’s valuation noting that Ryan Dahl, the creator of Node.js, was intending to create real-time websites with push capability, “inspired by applications like Gmail”. In Node.js, he has to give developers a tool for working in the non-blocking, event-driven I/O paradigm.

    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 censure: Node.js shines in real-time web purpose employing push technology past WebSockets. What is so radical about that? Well, after over 20 years of stateless-web based on the stateless request-response archetype, we assuredly have web applications with real-time, two-way connections, where twain the client and server can launch communication, Confessing them to interchange data freely. This is in stark opposition to the typical web response paradigm, where the client repeatedly initiates communication. Additionally, it’s all placed on the open web stack (HTML, CSS, and JS) running on the standard port 80

    One might argue that we’ve had this for years in the form of Flash and Java Applets—but in reality, those were just sandboxed environments using the web as a transport protocol to be conveyed to the client. In addition, they were run in isolation and again operated over non-standard ports, which may have appropriated extra permissions and alike.

    With all of its benefits, Node.js now plays a critical role in the technology stack of many high-profile companies which are depend on its specific amount of benefits.

    In this post, I’ll discuss not only how these advantages are getting, but also why you might want to use Node.js.

    How Does It Work?

    The main idea of Node.js: use non-blocking, event-driven I/O to go on lightweight and active in the face of data-intensive real-time function that runs beyond distributed devices.

    That’s a mouthful.
    What it actually means is that Node.js is not a silver-bullet new stage that will control the web development world. Instead, it’s a platform that ample a particular need.

    What it easily means is that Node.js is not a silver-bullet new staging that would dominate the web development world. Rather, it’s a platform that fills a particular need. And forgiving this is absolutely fundamental. You definitely don’t need to use Node.js for CPU-intensive operations; in fact, using it for huge computation will annul nearly all of its advantages. Where Node really shines is in framework fast, scalable network appliance, as it’s ability to handle a huge number of concurrent connections with high throughput, which equates to high scalability.

    How it works under-the-hood is quite interesting. Differentiating to traditional web-serving techniques where each connection (request) spawns a new thread, catching up system RAM and eventually maxing out at the quantity of RAM available, Node.js doing on a single-thread, using non-blocking I/O calls, own it to support tens of thousands of concurrent connections (held in the event loop).

    apcelent-nodejs-toptal
    apcelent-nodejs-toptal

    An abrupt calculation: domineering that each thread potentially has a concomitant 2 MB of memory with it, running on a system with 8 GB of RAM puts us at a theoretical maximum of 4000 concurrent connections, plus the cost of context-switching within threads. That’s the scenario you by an ordinary deal with in traditional web-serving techniques. By ignoring all that, Node.js achieves scalability levels of over 1M contemporaneous connections (as a proof-of-concept).

    There is, of course, the question of sharing a single thread between all clients requests, and it is a hidden pitfall of writing Node.js applications. Firstly, heavy computation could choke up Node’s single thread and cause problems for all clients (more on this later) as incoming desired would be blocked until said computing was completed. Secondly, developers need to be really careful not to allow an exception bubbling up to the core (topmost) Node.js event loop, which will cause the Node.js in place to terminate (effectively crashing the program).

    The technique used to bypass exceptions bubbling up to the outer is passing errors back to the caller as callback parameters (instead of throwing them, like in other environments). Even if some unhandled exception conduct to bubble up, there are multiple paradigms and tools available to auditor the Node process and perform the necessary recovery of a crashed example(although you won’t be able to recover users’ sessions), the most common being the Forever module or a different approach with external system tools upstart and monit.

    NPM: The Node Package Manager

    When discussing Node.js, one thing that clearly should not be omitted is built-in support for package management using the NPM tool that comes by default with every Node.js installation. The idea of NPM modules is quite comparable to that of Ruby Gems: a set of publicly available, reusable components, available through easy installation via an online repository, with version and dependency management.

    A full list of packaged schedule can be found on the NPM website https://npmjs.org/ or accessed using the NPM CLI tool that automatically gets installed with Node.js. The module ecosystem is open to all, and anyone can publish their own module that will be listed in the NPM repository. A brief introduction to NPM (a bit old, but still valid) can be found at http://howtonode.org/introduction-to-npm.

    Some of the most popular NPM modules today are:

    • express– Express.js, a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
    • connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
    • socket.io and sockjs – Server-side component of the two most common web sockets components out there today.
    • Jade – One of the popular templating engines, inspired by HAML, a default in Express.js.
    • mongo and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
    • redis – Redis client library.
    • coffee-script – CoffeeScript compiler that allows developers to write their Node.js programs using Coffee.
    • underscore (lodash, lazy) – The most popular utility library in JavaScript packaged to be used with Node.js, as well as its two counterparts, which promise better performance by taking a slightly different implementation approach.
    • forever – Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.

    The list goes on. There are tons of really useful packages out there, available to all (no offense to those that I’ve omitted here).

    Examples of Where Node.js Should Be Used

    CHAT

    Chat is the most typical real-time, multi-user appliance. From IRC (back in the day), through many recoveries and open protocols running on non-standard ports, to the ability to appliance everything today in Node.js with web sockets running over the standard port 80.

    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 on shared devices. It’s also a great use-case for learning too, as it’s simple, yet it covers most of the paradigms you’ll ever use in a typical Node.js application

    Let’s try to depict how it works.

    In the easiest example, we have a single chatroom on our website where people come and can exchange messages in one-to-many (actually all) fashion. For instance, say we have three people on the website all connected to our message board.

    On the server-side, we have a simple Express.js application which gadget two things:
    1) a GET ‘/’ request handler which serves the web page containing both a message board and a ‘Send’ button to initialize new message input, and
    2) a web sockets server that listens for new messages emitted by WebSocket clients.

    On the client-side, we have an HTML page with a couple of representative set up, one for the ‘Send’ button click event, which picks up the input message and sends it down the web socket, 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).

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

    1. Browser caches the ‘Send’ button click through a JavaScript handler, picks up the value from the input field (i.e., the message text), and emits a web socket 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 leading it to all other akin clients using the broadcast method.
    3. All clients receive the new message as a push message via a web socket client-side component running on 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.

      apcelent-nodejs
      apcelent-nodejs

    API ON TOP OF AN OBJECT DB

    Although Node.js really shines with real-time applications, it’s quite an innate fit for exposing the data from object DBs (e.g. MongoDB). JSON stored data allow Node.js to function without the impedance mismatch and data conversion.

    For instance, if you’re using Rails, you would convert from JSON to binary models, then disclose them back as JSON over the HTTP when the data is consumed by Backbone.js, Angular.js, etc., or even plain jQuery AJAX calls. With Node.js, you can simply expose your JSON objects with a REST API for the client to consume. Additionally, you don’t need to worry about converting between JSON and whatever else when reading or script from your database (if you’re using MongoDB). In sum, you can avoid the need for multiple conversions by using a uniform data serialization format across the client, server, and database.

    QUEUED INPUTS

    If you’re receiving a high quantity of concurrent data, your database can become a bottleneck. As characterized above, Node.js can easily handle the concurrent connections themselves. But because database access is a blocking operation (in this case), we run into stress. The solution is to acknowledge the client’s behavior before the data is truly written to the database.

    With that approach, the system preserving its responsiveness under a heavy load, which is particularly useful when the client doesn’t need firm confirmation of the successful data write. Typical examples include the logging or writing of user-tracking data, processed in amount and not used until a later time; as well as operations that don’t need to be reflected instantly (like updating a ‘Likes’ count on Facebook) where consequent consistency (so often used in NoSQL world) is acceptable.

    Data gets queued through some kind of caching or notice queuing infrastructure (e.g., RabbitMQ, ZeroMQ and digested by a separate database batch-write process, or estimating intensive processing backend services, written in a better-performing dais for such tasks. Similar behavior can be implemented with other languages/frameworks, but not on the same hardware, with the same high, maintained throughout.

    apcelent-nodejs
    apcelent-nodejs

    In short: with Node, you can push the index database writes off to the side and deal with the next, proceeding as if they succeeded.

    DATA STREAMING

    In more traditional web platforms, HTTP requests and responses are treated like an 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, and proxying between different data sources (see next section).

    PROXY

    Node.js is easily employed as a server-side proxy where it can handle a lot of simultaneous connections in a non-blocking manner. It’s especially useful for proxying different benefit with different response times or collecting data from multiple source points.

    An example: consider a server-side application communicating with third-party resources, pulling in data from different sources, or storing assets like images and videos to third-party cloud services.

    Although devoted proxy servers do exist, using Node instead might be helpful if your proxying infrastructure is non-existent or if you need a solution for local development. By this, I mean that you could build a client-side app with a Node.js development server for assets and proxying/stubbing API requests while in production you’d handle such interactions with a dedicated proxy service (Nginx, HAProxy, etc.).

    BROKERAGE – STOCK TRADER’S DASHBOARD

    Let’s get back to the application level. Another example where desktop software dominates but could be easily replaced with a real-time web solution is brokers’ trading software, used to track stocks prices, perform calculations/technical analysis, and create graphs/charts.

    Switching to a real-time web-based explanation would allow brokers to easily switch workstations or working site. Soon, we might start seeing them on the beach in Florida.. or Ibiza.. or Bali.

    APPLICATION MONITORING DASHBOARD

    Another common use-case in which Node-with-web-sockets fits perfectly: tracking website visitors and visualizing their interactions in real-time. (If you’re interested, this idea is already being productized by Hummingbird.)

    Imagine how you could improve your field if you knew what your visitors were doing in real-time—if you could visualize their interactions. With the real-time, two-way sockets of Node.js, now you can.

    Where Node.js Can Be Used

    SERVER-SIDE WEB APPLICATIONS

    Node.js with Express.js can also be used to create classic web applications on the server-side. However, while possible, this request-response paradigm in which Node.js would be given around rendered HTML is not the most typical use-case. There is brawl to be made for and against this approach. Here are some facts to consider:

    Pros:

    • If your application doesn’t have any CPU intensive computation, you can build it in Javascript top-to-bottom, even down to the database level if you use JSON storage Object DB like MongoDB. This eases development (including hiring) significantly.
    • Crawlers receive a fully-rendered HTML response, which is far more SEO-friendly than, say, a Single Page function or a web sockets app run on top of Node.js.

    Cons:

    • Any CPU intensive computation will block Node.js responsiveness, so a threaded platform is a better approach. Alternatively, you could try scaling out the computation [*].
    • Using Node.js with a relational database is still quite a pain (see below for more detail). Do yourself a favor and pick up any other environment like Rails, Django, or ASP.Net MVC if you’re trying to perform relational operations. [*] An alternative to this CPU intensive estimation is to create a highly scalable MQ-backed environment with back-end processing to keep Node as a front-facing ‘clerk’ to handle client requests asynchronously.

    Conclusion

    We’ve considered Node.js from theory to model, beginning with its destination and ambitions, and ending with its sweet spots and risk. When people run into problems with Node, it relatively always boils down to the fact that the blocking procedure is the root of all evil—99% of Node misuses come as a direct consequence.

    Remember: Node.js was never created to solve the compute scaling problem. It was initiated to solve the I/O scaling problem, which it does really well.

    See more:

    Most popular node js framework 2016

    Previous articleErmm .. Programmers
    Next articleHow times change!

    Latest Articles

    More Recipes Like This