Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

JavaScript: Asynchronous Programming with Node.js

In this article we will discuss good practices for the asynchronous development in JavaScript, using Node.js.

When we think about front-end development, Node.js is one of the technologies that most intrigues and impresses the development community, for its number of key features such as being able to use JavaScript on the server, for example.

For those already familiar with JavaScript, Node.js is just a short step to learning. Everything on the web is about consumerism and content production. Reading or writing blogs, watch or upload videos, view and post pictures, listen to music and so on.

We do this naturally every day on the internet. The need is increasing for such interaction between users with different web services.

In fact, the whole world wants to interact more and more on the Internet, either through conversations with friends in chat rooms, playing online games, constantly updating their social networks or participating in collaborative systems.

These types of applications require extremely fast processing power to be effective in real-time interaction between client and server. Moreover, this needs to happen on a massive scale, supporting hundreds of millions of users.

So what should developers do? We need to create a real-time communication between client and server - that is fast, work for many users at the same time and use I/O (input and output devices) resources efficiently. Anyone with experience in web development knows that the current version of HTTP 1.1 is not designed to support such requirements.

And worse, unfortunately, there are applications that adopt incorrectly using this protocol, implementing workaround solutions that constantly ordering the server asynchronously, usually by applying the long-polling technique.

For systems work in real time, servers need to send and receive data using two-way communication, instead of using intensely request / response HTTP applying Ajax. And we also have to keep such communication light and quick to allow scalability in an application.

The problem of blocking architectures

The systems developed on web platforms such as .NET, Java, PHP, Ruby or Python have one thing in common: they paralyze processing while using an I/O on the server. This shutdown is known as blocking-thread model.

To better understand this concept, let's do an example: have a web application and each of its processes is a request made by the user.

As the application goes by, new users will access it, and create multiple processes on the server. A blocking system architecture will queue each request which is held at the same time and then it will suite one by one.

This model allows multiple processes simultaneously. While a request is processed, the remaining are on standby. That is, the application blocks remaining to process I/O server, keeping them in a short time in a queue of idle requests.

This is a classic architecture, existing in various web systems and has an inefficient design. It spends much of its time keeping requests in a queue idle while running I/O for just one request. I/O examples include email tasks, consult the database, reading disk files.

And these tasks block the entire system as they are not finalized. With the increase of users accessing the system, the frequency of bottlenecks will be higher, resulting in the need to make a vertical scalability (upgrade of servers) or horizontal scalability (addition of new servers working for a load balancer). Both types become costly when it comes to infrastructure spending.

It is better to look for new technologies that make good use of existing servers, allowing the intense and maximum use of the current processing.

Based on this problem, in late 2009, Ryan Dahl, and 14 employees, created Node.js This technology features an innovative model: its architecture is fully non-blocking thread which presents a good performance with memory consumption and maximum use and efficiently the processing power, of servers, especially in systems that produce a high load.

Systems built with Node.js are free from waiting for a long time the result of its processes, and most importantly, do not suffer from dead-locks, simply by working in single-threaded. In addition to these advantages, develop systems with super simple and practical platform.

Node.js is a highly scalable platform and low-level, so you will be able to program directly with various network protocols and internet use libraries or accessing operating system resources. To program in Node.js, just master the JavaScript language. It uses the V8 JavaScript engine, the same used in Google Chrome browser.

Node.js Features

Single-thread

Its applications are single-threaded, meaning each application instance will have a single thread per process started. If you are used to working with multithreaded programming, such as Java or .NET, unfortunately won't be possible with Node.js, but know that there are other ways to create a system with parallel processing.

For example, you can use a native library called clusters, which is a module that allows you to deploy a network of processes of your application, you create N processes for your application and one of them is responsible for balancing the load, allowing parallel processing a single server. Another way is to adopt asynchronous programming in tasks of your server.

This will be the most discussed subject during the course of this article, by which we will explain various scenarios and practical examples of how they are executed in parallel, functions running asynchronously.

Event-Loop

Node.js is event-driven. It follows the same guiding philosophy of browser JavaScript events; the only difference is the event, that is, no click mouse events, keyboard keyup or HTML components event.

In fact, we work with I/O and events. For example, connect to the database, open a file, streaming data and many others.

The Event-Loop is the agent responsible for listening and committing events. In practice, it is basically an infinite loop that checks each iteration in your event listening queue to see if an event was triggered. It issues an event executes it and sends it to the queue to run.

When an event is running, we can develop any logic in it and it all happens thanks to the JavaScript function callback mechanism.

This technique allows you to work on the event-driven design with Node.js tt was inspired by frameworks Event Machine of Ruby and Twisted of Python. However, Node.js of Event-loop is better in performance because your engine is natively implemented in a non-blocking manner.

This makes it a great advantage over its competitors which perform blocking calls to start their respective loop events.

Why we should we learn Node.js?

  • JavaScript everywhere:

    Node.js uses JavaScript as server-side programming language. This feature allows you to reduce your learning time. After all, the language is the same as the client-side JavaScript, its challenge on this platform will be to learn the background how asynchronous programming to get the most out of this technique in your application.

    Another advantage of working with JavaScript is that it will be easy for you to maintain your projects. Will be easy to find professionals for your projects, and you will spend less time studying a new server-side language. A technical advantage of JavaScript in comparison with other back-end languages is that you will no longer use those frameworks serialization of JSON objects (JavaScript Object Notation), after the JSON client-side is the same on the server-side, there are also cases applications using database-driven documents (eg MongoDB or CouchDB) and in this case all data manipulation is performed using JSON objects as well.

  • Active community:

    This is one of the strengths of Node.js. Currently there are several communities around the world working hard for this platform, is touting posts and tutorials, speaking at events and especially publishing and maintaining +70000 modules on site NPM (Node Package Manager).

  • Great salaries:

    Node.js developers usually receive good salaries. This is the fact that unfortunately in some countries there are still few companies adopting this technology. This makes companies that need this technology to pay salaries in average or above average to keep thesedevelopers in their projects.

    Another interesting case are the companies that hire interns or junior programmers who have at least basic knowledge of JavaScript, in order to train them to work with Node.js In this case, do not expect a high salary but a broad knowledge filling your resume.

  • Ready for real-time:

    Node.js became popular thanks to its frameworks of real-time interaction between client and server. The SockJS, Socket.IO, Engine.IO are some examples. They are compatible with the recent WebSockets protocol and allow to traffic data via a single two-way connection, treating all messages through JavaScript events.

  • Big players:

    LinkedIn, Walmart, Groupon, Microsoft and Paypal are some of the companies currently using Node.js.

Where to use Node.js?

Chats

A chat is the most typical example of multi-user real-time application. Since IRC to many proprietary and open protocols on non-standard ports, even the ability to implement everything in today Noje.js with WebSockets running on the same default port 80.

A chat application is really a great example of being used with Noje.js: it is lightweight, has high traffic data, but requires little processing/computing and running among multiple devices.

After all, is very simple, great for developers who are starting to learn a technology, covering most of the paradigms used by the language. Basically, such an application works in a ready website domain where people can go and be used to exchange messages among themselves connected by a programming structure and networks. On the server side, we have a simple application that implements two things:

  • A GET request that serves the web page containing both message and submit button to initialize a new message entry;
  • WebSockets listening for new messages sent by their customers.

On the client side, we have an HTML page with a number of configured handlers, one for the Send button, selecting the message and sending it to the WebSocket, and one that listens for messages that are coming on the client. Obviously, this is a simple and basic model, but based on the other variance to its complexities.

API on a DB object

Although Node.js really shines with real-time applications, it is natural to have to expose the data from a database object (MongoDB, for example). JSON data stored allows Node.js to work without the impedance mismatch and data conversion.

For example, if you are using Rails, you must convert JSON to binary models and then exposing them back as JSON over HTTP when data is consumed by frameworks like Backbone.js, Angular.js, etc. or even by a simple jQuery Ajax calls.

With Node.js, you can simply expose your JSON objects with a REST API for the client to consume. In addition, you do not have to worry about conversion between JSON and everything you read or write in your database (if using MongoDB). In short, you can avoid the need for multiple conversions using a uniform data serialization format across the client and database server.

Queue entries

If you are getting a lot of concurrent data, the database can become a bottleneck. Node.js can easily handle its own simultaneous connections. But because access to databases is a blocking operation, we have to look for other solutions. The solution is to recognize the customer's behavior before the data is actually written to the database.

With this approach, the system maintains its responsiveness under a heavy load, which is particularly useful when the client does not need firm confirmation of a successful data recording.

Typical examples include registration or recording user tracking data, batch processing, and not used until later; and operations that do not need to be reflected instantly (like updating a count 'Likes' on Facebook), where the eventual consistency (often used in the NoSQL world) is acceptable.

The data is queued for some kind of cache or infrastructure Message Queuing (eg RabbitMQ, ZeroMQ) and digested by a separate database batch process and writing, or compute-intensive back-processing services end, written in a better performance platform for such tasks. Similar behavior can be implemented with other languages / frameworks, but not in the same hardware with the same high performance kept.

Data flow

In more traditional web platforms, HTTP requests and responses are treated as isolated events; in fact, they are actually streams. This observation can be used in Node.js to build some interesting features.

For example, you can process files while they are still being loaded, since the data comes in the middle of a stream and can process them online form. This could be done for real-time audio or video encoding and proxy between the different data sources.

Proxy

Node.js is easily used as a proxy server side, where it can handle a large amount of concurrent connections a non-blocking manner.

It is especially useful for proxies of different services with different response times or data collection from multiple points of origin.

Consider as an example a server-side application that communicates with third party funds, pulling data from different sources or storing assets such as images and videos for cloud services from third parties.

While there are dedicated proxy servers, using Node.js, instead, can be useful if your proxy infrastructure is lacking or if you need a solution for local development. So you can build a client-side application with a Node.js development server for active and proxy requests/Stubbing API, while production would you handle such interactions with a dedicated service proxy (nginx, HAProxy, etc. .).

Application Monitoring Panel

Another common case, where the Node with WebSockets fits perfectly is the tracking of site visitors and display their interactions in real time.

You could gather real-time statistics from your server, or even move it to the next level by introducing specific interactions with your visitors, opening a channel of communication when they reach a specific point in your filter.

Imagine how you can improve a specific business, if you can learn what visitors were doing in real time, if you can see their interactions. With real-time, two-way basis of Node.js, it is now possible.

Systems Monitoring Panel

In this case we see a little side infrastructure. Imagine, for example, a SaaS provider that wants to offer its users a monitoring service page (eg GitHub status page). With Node.js circuit event, we can create a powerful web-based panel that checks the states of asynchronous services and sends data to customers using WebSockets.

Both internally and the status of public services can be reported live and in real time, using the NodeJS technology. Going one step further with this idea and trying to imagine a Network Operations Center (NOC) monitoring applications in a service provider, cloud / network / hosting provider, or any financial institution, all run on the open web stack supported by the Node.js and WebSockets instead of Java and/or Java Applets, as was seen for a long time.

Hands on Node.js

To configure a Node.js environment, regardless of which operating system, the tips are the same. Only some procedures will be different for each system, especially for Windows, but will not be anything serious.

NOTE: All codes used in this article will work only in versions of Node.js 0.8.x or higher.

Installation

The first step is to visit the official website (see Links). Then click Install to automatically download the latest version compatible with your operating system (if your system is Windows or MacOS).

If you use Linux recommend you read in detail the Wiki Node.js repository which explains the main instructions on how to install it via a package manager of a Linux distribution.

Now install it and if no problems occur, just open your terminal or command prompt and type the following command to see their versions of Node.js and NPM that were installed:

node -v && npm -v

The latest stable version being used in this article is Node v0.10.31 and NPM 1.4.23.

Setting up the development environment

To configure the development environment just add NODE_ENV environment variable in the operating system. On Linux or MacOS, simply login with a text editor and super user mode (sudo) the .bash_profile or .bashrc file and the end of file add the following command line:

export NODE_ENV=’development’

Right-click the My Computer icon and select Properties and on the left side of the window click the link Advanced System Settings. In the next window, go to the Advanced tab and click the Environment Variables ... button.

Now in the field of system variables click the New button ... and the variable name field type NODE_ENV and the value of the variable field type development. After completing this task, restart your computer to automatically load that variable in the operating system.

Running Node.js

To test the environment, execute our first Hello World program. Open your terminal or command prompt and run the command node. This command will access the REPL mode (Read-Eval-Print-Loop) that allows you to run JavaScript code directly by a black screen. Now type console.log ("Hello World") and press ENTER to run it on time.

Managing dependencies and modules using the NPM

As RubyGems Ruby or Maven Java, Node.js also has its own package manager, it is called NPM. He became so popular in the community, which was from the 0.6.x version that was integrated into Node.js installer, making it the default manager of this platform.

This simplified the lives of developers at the time., Iit made several projects to converge for this platform until the present day. Using the NPM is very easy, so let's see the main commands so that you have notions of how to use them:

  • npm install name-of-module: install a module in the project;
  • npm install -g name-of-module: install a global module;
  • npm install name-of-module --save: install the module in the project, updating the package.json in dependency list;
  • npm list: lists all modules of the project;
  • -g npm list: list all global modules;
  • npm remove name-of-module: uninstall a project module;
  • npm remove -g name-of-module: uninstall a global module;
  • npm update name-of-module: updates the module version;
  • npm update -g name-of-module: updated version of the global module;
  • npm -v: Displays the current version of npm;
  • npm adduser-name User: create an account on npm through the NPM Site;
  • npm whoami: Displays details of your public profile npm (you must create an account before);
  • npm publish: publish a module in npm website (you must have an account before NPM).

Understanding the package.json

All Node.js project is called module, but what is a module? As you read, note that we will discuss much about the term module, library and framework, and in practice they have the same meaning.

The term module emerged the concept that JavaScript works with a modular architecture. And every module is accompanied by a file descriptor, known for package.json name.

This file is essential for Node.js project. A poorly written package.json can cause bugs or prevent proper operation of your module, because it has some key attributes that are understood by Node.js and NPM.

In Listing 1 we have a package.json containing the key attributes to describe a module.

Listing 1. Format of a package.json.

{ 
"name": "my-first-node-app",
 "description": "My first app Node.js", 
"author": "Nick <nick@email.com>", 
"version": "1.2.3", "private": true, 
"dependencies": { 
"module-1": "1.0.0", 
"module-2": "~1.0.0", 
"module-3": ">=1.0.0" 
}, 
"devDependencies": { 
"module-4": "*" 
} 
}

With these attributes, you already described as little as possible what will be its implementation. The name attribute is the principal. With it, you describe the project name, the name by which your module will be called via function require ('my-first-node-app'). In description, describe what will this module. It should be written in short and clear, providing a module of the abstract.

The author is an attribute to provide the name and email of the author. Use the Name format so that sites like npm correctly recognizes that data. Another key attribute is the version with which we define the current version of the module. It is strongly recommended to have this attribute, it is not impossible to install the module via npm command.

The private attribute is a Boolean, and determines whether the project will open or private code to download the npm.

Node.js modules work with three levels of versioning. For example, version 1.2.3 is divided into levels: Major (1) Minor (2) and patch (3).

Note that the field dependencies were included four modules, each of which used a different way of defining the design version. The first modulo-1 is included only its fixed version 1.0.0. Use this type version to install dependencies, which updates can break the project for the simple fact that certain features were removed and still use them in the application.

The second module has already developed update flexibility. It uses the "~" character that performs updates patch level (1.0.x). Generally these updates are safe, just bringing improvements or bug fixes.

The module-3 updated versions that are greater than or equal to version 1.0.0 on all levels. In many cases, use ">=" can be dangerous, because addiction can be updated major or minor level, containing large changes that can break a production system, compromising its operation and requiring you to update all the code to return to normal.

The last, the module-4, uses the character "*"; this always pick the latest version of the module at any level. It can also cause updates and has the same module-3 versioning behavior.

Generally it is used in devDependencies, which are focused dependencies for testing or exclusively for development environment, and updates of modules not affect the system behavior that is already in the air.

Scopes of local and global variables

As in the browser, use the same JavaScript in Node.js It also uses local and global scopes of variables. The only difference is in how the scopes of global variables are implemented. In the browser, global variables are created the way can be seen in Listing 2.

Listing 2. Global variables in the client-side.

  window.today = new Date();   alert(window.today);

In any browser, the keyword window allows you to create global variables that are accessed anywhere. You Node.js, we use the global keyword to apply this same technique (Listing 3).

Listing 3. Global variables in the server-side.

     global.today = new Date();   console.log(global.today);

By using global maintain a global variable, accessible from anywhere in the project without the need to call it via require or pass it as parameter in a function. This concept of global variable exists in most programming languages, as well as its use so it is recommended to work with the least possible global variables to prevent future memory bottlenecks in the application.

Threads vs. asynchrony

As much as asynchronous functions can run in parallel multiple tasks, they will never be considered one thread (for example Java Threads). The difference is that threads are manipulated by the developer, or you can pause the execution of a thread or make her wait the end of another thread to run.

Asynchronous calls only invoke its functions and you do not control them, only works with its returns through a callback function.

It may seem advantageous to have control over the execution of threads in favor of a system that performs tasks in parallel, but little knowledge about them can turn your system into a crashing chaos of dead-locks, after all they are executed in a blocking manner.

This is the great advantage of asynchronous calls., Tthey perform their duties in parallel without locking processing of other and mainly without blocking the application.

It is essential that your Node.js code invokes the least possible blocking functions. All synchronous function will prevent at that moment that Node.js continue running other code until that function is finished.

For example, if this function makes an I / O to disk, it will block the whole system, leaving the processor idle as he uses other server resources, such as disk reading, network usage etc. Whenever you can, use asynchronous functions to take advantage of this key feature of Node.js

In fact Node.js is an excellent choice for front-end developers know a little about back-end without having to learn a new language, after all this platform the same client-side JavaScript, just with some different details.

Another important detail is about the advantages of asynchronous functions and their I/O non-blocking. After all as we have seen in a benchmark test an action of I/O simple that was a reading of a single file of about 7 MB and the locking time was much shorter than a blocking read.

Now imagine this same action on a large scale, reading multiple files of 1 GB or while performing multiple I/O’s on your server to thousands of users, all without blocking the application. This is one of the strengths of Node.js!

Hope you all enjoyed the article, we’ll see you next time.

Links

Official NodeJS website

http://www.nodejs.org/


Freelancer Software Developer. Have knowledge in Java, Android, HTML, CSS and Javascript. He has also knowledge in Agile Development

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login