search
HomeWeb Front-endJS TutorialNode.js event driven_node.js

Overview of Node.js event-driven implementation

Although "events" are not (and are not necessary) clearly defined in the ECMAScript standard, events serve as an extremely important mechanism in browsers, giving JavaScript the ability to respond to user operations and DOM changes; in Node. In js, the asynchronous event-driven model is the basis of its high concurrency capability.

Learning JavaScript also requires understanding its running platform. In order to better understand the event model of JavaScript, I plan to start with Node and browser engine source code, analyze its underlying implementation, and organize my analysis into a series of blog posts; On the one hand, it is a note, and on the other hand, I hope to communicate with everyone. If there are any omissions and biases in the analysis and understanding, I hope you will correct me.

A brief description of the event-driven model

There are already many good articles explaining the JavaScript event model itself. It can be said that this is already a poorly discussed topic. Here I will only briefly write about it and provide links to some good articles.

How the program responds to events

Our program responds to external events in the following two ways:

Interruption

The operating system handles keyboard and other hardware input through interrupts. The advantage of this method is that even without multi-threading, we can safely execute our code. After the CPU receives the interrupt signal, it will automatically transfer to execute the corresponding After the interrupt handler is completed, the execution environment of the original code will be restored and execution will continue. This method requires hardware support and is generally encapsulated by the operating system.

Polling

Loop to detect whether an event occurs, and if so, execute the corresponding handler. This applies to both low-level and upper-level development.
Windows window programs need to write the following code in the main thread, usually called a message loop:

MSG msg = { };
while (GetMessage(&msg, NULL, 0, 0))
{
  TranslateMessage(&msg);
  DispatchMessage(&msg);
}

The message loop continuously detects whether there are messages (user's UI operations, system messages, etc.). If so, it distributes the messages and calls the corresponding callback function for processing.
One disadvantage of the polling method is that if time-consuming operations are performed in the message loop of the main thread, the program cannot respond to new messages in a timely manner. This is evident in JavaScript and will be mentioned later, along with its solutions.

However, there is no similar message loop code in JavaScript. We simply register the event and wait for it to be called. This is because the browser and Node, as execution platforms, have already implemented the event loop. JavaScript code does not need to be involved in this process. It only needs to wait quietly as the callee.

Event loop in Node

Look at the implementation of event loop through Node source code

Node uses V8 as the JavaScript execution engine and uses libuv to implement event-driven asynchronous I/O. Its event loop uses libuv's default event loop.

In src/node.cc,

Environment* env = CreateEnvironment(
    node_isolate,
    uv_default_loop(),
    context,
    argc,
    argv,
    exec_argc,
    exec_argv);

This code establishes a node execution environment. You can see uv_default_loop() in the third line. This is a function in the libuv library. It initializes the uv library itself and the default_loop_struct in it, and returns a pointer to it. Pointer default_loop_ptr.
Afterwards, Node will load the execution environment and complete some setup operations, and then start the event loop:

bool more;
do {
 more = uv_run(env->event_loop(), UV_RUN_ONCE);
 if (more == false) {
  EmitBeforeExit(env);
  // Emit `beforeExit` if the loop became alive either after emitting
  // event, or after running some callbacks.
  more = uv_loop_alive(env->event_loop());
  if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
   more = true;
 }
} while (more == true);
code = EmitExit(env);
RunAtExit(env);
...

more is used to identify whether to proceed to the next cycle.

env->event_loop() will return the default_loop_ptr previously saved in env, and the uv_run function will start the event loop of libuv in the specified UV_RUN_ONCE mode. In this mode, uv_run will process at least one event: this means that if there are no I/O events that need to be processed in the current event queue, uv_run will block until there is an I/O event that needs to be processed, or the next timer Time's up. If there are currently no I/O events and no timer events, uv_run returns false.

Next Node will decide the next step based on the situation of more:

If more is true, continue running the next loop.

If more is false, it means that there are no events waiting to be processed. EmitBeforeExit(env); triggers the 'beforeExit' event of the process, checks and processes the corresponding processing function, and jumps out of the loop directly after completion.

Finally, the 'exit' event is triggered, the corresponding callback function is executed, the Node operation ends, and some resource release operations will be performed later.

In libuv, timer events are processed directly in the event loop, while I/O events are divided into two categories:

Network I/O uses the non-blocking I/O solution provided by the system, such as epoll on Linux and IOCP on Windows.

There is no (good) system solution for file operations and DNS operations, so libuv built its own thread pool to perform blocking I/O.

In addition, we can also throw the custom function into the thread pool to run. After the operation is completed, the main thread will execute the corresponding callback function. However, Node has not added this function to JavaScript, which means that it only It is impossible to open new threads in JavaScript for parallel execution using native Node.

The above is the entire content of this article, I hope you all like it.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript in Action: Real-World Examples and ProjectsJavaScript in Action: Real-World Examples and ProjectsApr 19, 2025 am 12:13 AM

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

JavaScript and the Web: Core Functionality and Use CasesJavaScript and the Web: Core Functionality and Use CasesApr 18, 2025 am 12:19 AM

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools