


Detailed explanation of event listening and event publishing usage examples in Node.js
node.js is based on single-threaded non-blocking asynchronous I/O. Asynchronous I/O means that when encountering an I/O operation, the thread does not block but performs the following operations. , then after the I/O operation is completed, how does the thread know that the operation is completed?
When the operation completes the time-consuming I/O operation, the thread of the I/O operation will be notified in the form of an event. The thread will process the event at a specific time and proceed to the next step. Operation, in order to complete asynchronous I/O, the thread must have an event loop mechanism, constantly insisting on whether there are unfinished events, and completing the processing of these events in sequence.
For blocking I/O, when a thread encounters a time-consuming I/O operation, it will stop executing and wait for the operation to be completed. At this time, the thread cannot accept other operation requests. In order to provide throughput, Multiple threads must be created, each thread to respond to a customer's request, but at the same time, only one thread can run on a CPU core. If multiple threads want to execute, they must switch between different threads.
Therefore, node.js reduces the cost of thread creation and thread switching in multi-threads. The cost of thread switching is very high. It needs to allocate memory for it and include it in the schedule. At the same time, when thread switching Sometimes it is necessary to perform memory paging and other operations. These operations can be reduced by using a single thread. However, this programming method also has shortcomings and is not in line with people's design thinking.
node.js is based on the event mode to implement asynchronous I/O. When it is started, it will continuously traverse whether there are any completed events, and then execute them. After the execution is completed, another event will be Notify the thread in the form that this operation has been completed, and this event will be added to the unfinished event list. The thread will traverse this event at some point in the next time and then execute it. In this mechanism, a large event needs to be The tasks are divided into small events, and node.js is also suitable for handling some high I/O and low logic scenarios.
The following example demonstrates asynchronous file reading:
##
var fs = require('fs'); fs.readFile('file.txt', 'utf-8', function(err, data) { if (err) { <span style="white-space:pre"> </span>console.error(err); } else { <span style="white-space:pre"> </span>console.log(data); } }); [javascript] view plain copy console.log("end");
fs. readFile reads the file asynchronously, and then the process will continue without waiting for it to finish reading the file. When the file is read, an event will be released, and the execution thread will execute the corresponding event when it traverses the event. Operation, here is to execute the corresponding callback function. In the example, the string end will be printed out before the file content.
node.js event API
events.EventEmitter: EventEmitter emits events in node.js with The event listening function provides encapsulation. Each event consists of a string identifying the event name and the corresponding operation.
Event monitoring:
var events = require("events"); var emitter = new events.EventEmitter(); <span style="font-family: Arial, Helvetica, sans-serif;">emitter.on("eventName", function(){</span> console.log("eventName事件发生") })
Event publishing:
emitter.emit("eventName");
EventEmitter.once("eventName", listener) : Register a listener for the event that only executes once. When the event occurs for the first time and the listener is triggered, the The listener will be dismissed, and if the event occurs later, the listener will not be executed.
EventEmitter.removeListener(event, listener) :Remove the event listener
EventEmitter.removeAllListeners(event): Remove all event listeners
EventEmitter.setMaxListeners(n): node.js default maximum number of listeners for a single event is 10. If A warning will be given if it exceeds 10. This is done to prevent memory overflow. We can change this limit to another number. If it is set to 0, it means no limit.
EventEmitter.listeners(event) : Returns a list of listeners for an event
Collaboration between multiple events In slightly larger applications, the separation between data and web servers is inevitable, such as Sina Weibo, Facebook, Twitter, etc. The advantage of this is that the data sources are unified, and various rich client programs can be developed for the same data sources.
api.getUser("username", function (profile) { // Got the profile }); api.getTimeline("username", function (timeline) { // Got the timeline }); api.getSkin("username", function (skin) { // Got the skin });
api.getUser("username", function (profile) { api.getTimeline("username", function (timeline) { api.getSkin("username", function (skin) { // TODO }); }); });
为解决这类问题,我曾写作一个模块来实现多事件协作,以下为上面代码的改进版:
var proxy = new EventProxy(); proxy.all("profile", "timeline", "skin", function (profile, timeline, skin) { // TODO }); api.getUser("username", function (profile) { proxy.emit("profile", profile); }); api.getTimeline("username", function (timeline) { proxy.emit("timeline", timeline); }); api.getSkin("username", function (skin) { proxy.emit("skin", skin); });
EventProxy也是一个简单的事件侦听者模式的实现,由于底层实现跟Node.js的EventEmitter不同,无法合并进Node.js中。但是却提供了比EventEmitter更强大的功能,且API保持与EventEmitter一致,与Node.js的思路保持契合,并可以适用在前端中。
这里的all方法是指侦听完profile、timeline、skin三个方法后,执行回调函数,并将侦听接收到的数据传入。
利用事件队列解决雪崩问题
所谓雪崩问题,是在缓存失效的情景下,大并发高访问量同时涌入数据库中查询,数据库无法同时承受如此大的查询请求,进而往前影响到网站整体响应缓慢。
那么在Node.js中如何应付这种情景呢。
var select = function (callback) { db.select("SQL", function (results) { callback(results); }); };
以上是一句数据库查询的调用,如果站点刚好启动,这时候缓存中是不存在数据的,而如果访问量巨大,同一句SQL会被发送到数据库中反复查询,影响到服务的整体性能。一个改进是添加一个状态锁。
var status = "ready"; var select = function (callback) { if (status === "ready") { status = "pending"; db.select("SQL", function (results) { callback(results); status = "ready"; }); } };
但是这种情景,连续的多次调用select发,只有第一次调用是生效的,后续的select是没有数据服务的。所以这个时候引入事件队列吧:
var proxy = new EventProxy(); var status = "ready"; var select = function (callback) { proxy.once("selected", callback); if (status === "ready") { status = "pending"; db.select("SQL", function (results) { proxy.emit("selected", results); status = "ready"; }); } };
这里利用了EventProxy对象的once
方法,将所有请求的回调都压入事件队列中,并利用其执行一次就会将监视器移除的特点,保证每一个回调只会被执行一次。对于相同的SQL语句,保证在同一个查询开始到结束的时间中永远只有一次,在这查询期间到来的调用,只需在队列中等待数据就绪即可,节省了重复的数据库调用开销。由于Node.js单线程执行的原因,此处无需担心状态问题。这种方式其实也可以应用到其他远程调用的场景中,即使外部没有缓存策略,也能有效节省重复开销。此处也可以用EventEmitter替代EventProxy,不过可能存在侦听器过多,引发警告,需要调用setMaxListeners(0)
移除掉警告,或者设更大的警告阀值。
The above is the detailed content of Detailed explanation of event listening and event publishing usage examples in Node.js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to monitor the scrolling of an iframe requires specific code examples. When we use the iframe tag to embed other web pages in a web page, sometimes we need to perform some specific operations on the content in the iframe. One of the common needs is to listen for the scroll event of the iframe so that the corresponding code can be executed when the scroll occurs. The following will introduce how to use JavaScript to monitor the scrolling of an iframe, and provide specific code examples for reference. Get the iframe element First, we need

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
