Node JS executes in a single process and single thread only, but the execution performance is very high because of event-driven design and callback function usage. This article will introduce the Node JS event loop mechanism and tell you how to create custom events in Node JS by example.
1. Node JS Event Loop Introduction.
- When you execute an asynchronous method provided by the Node JS framework, you should provide a callback function to the method. You can continue to execute other js codes without having to wait for the method to complete.
- The callback function is an observer of the event queue. When the task is complete, the Node JS framework will add the event back to the event queue, then the callback function which just observes the event in the queue will be invoked to process the result.
- An example of this design pattern is Node JS implemented HTTP web server. The HTTP web server starts and listens for requests. When a request arrives, it will create an event in the queue and then waiting for the next request.
- When the previous request process is complete, the callback function will be invoked to send the response back to the client.
- This pattern is very high performance because web servers just waiting for requests and do not do other things like IO operation which will cost time and resources.
- You can read the article Use Node.js To Create HTTP Web Server And Process Web Page Example to learn more.
2. Create And Listen Custom Event In Node JS.
- If you find Node JS built-in event is not enough for you, you can create a custom event and bind the custom event process function with the event follow the below steps.
- Include Node JS events built-in module.
var events = require('events');
- Create an EventEmitter object use the above module.
var event_emitter = new events.EventEmitter();
- Create a JavaScript function as a callback function that will be triggered when the custom event happen.
var data_receive_handler = function(){ console.log('Data received.'); }
- Register custom events with the callback function. The callback function will observe the custom event.
event_emitter.on('receive_data', data_receive_handler);
- Trigger custom event then the callback function will be executed.
event_emitter.emit('receive_data');
3. Node JS Custom Event Example.
- custom-event.js
// Include node js prebuilt events module. var events = require('events'); // Create event emitter object. var event_emitter = new events.EventEmitter(); // Create connect event process function. var connect_handler = function connected() { console.log('Connect success.'); // Trigger receive_data event event_emitter.emit('receive_data'); } // Bind custom event connect with connect_handler process function. event_emitter.on('connect', connect_handler); // Create receive data event process function. var data_receive_handler = function(){ console.log('Data received.'); } // Bind custom event receive_data with data_receive_handler function. event_emitter.on('receive_data', data_receive_handler); // Trigger connect event. event_emitter.emit('connect'); console.log("Code exit");
- Run the below command to execute the above javascript source code in Node.JS.
$ node custom-event.js
- Below is the above example output.
Connect success. Data received. Code exit