Skip to content

The Node.js Platform

Updated: at 10:50 PM

Table of Contents

Open Table of Contents

The Node.js Platform

How Node.js works

I/O Operation

I/O (Input/Output) operation is a fundamental operation performed by a computer, whereby the computer reads data, process it, and outputs the results.

I/O operations are slow, because of the layer of communication between the computer and the input/output devices.

Types of I/O operations:

Blocking I/O operation blocks or waits for the operation to complete before proceeding to the next operation. The thread or process that issued the I/O operation is blocked and cannot perform any other operation until the data transfer is complete.

“Thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter (PC), a register set, and a stack. Consider it as a basic unit of work that is scheduled by the CPU. All the necessary resources needed for that unit of work is allocated in when the thread is created.”

  • Abraham Silberschatz, Greg Gagne, and Peter B, Operating System Concepts

Non-blocking I/O operation allow the program to continue executing while the data transfer is still in progress. The thread that issued the I/O operation is not blocked and can perform other operations while the I/O operation is still in progress.

Methods for processing non-blocking resources:

The Reactor Pattern

“The main idea behind the reactor pattern is to have a handler associated with each I/O operation. A handler in Node.js is represented by a callback (or cb for short) function.”

The reactor pattern is the heart of the asynchronous construct of a Node.js application whereby the demultiplexer in a blocking manner waits and reacts to I/O operation events. As soon as an I/ operation event occurs (like data arrival on network socket) that the demultiplexer is monitoring, the demultiplexer reacts by unblocking, and pushes the event(s) into the event queue. The event loop polls over the items in the event queue, and invokes the handler (function) in each event.