Node Js Event Loop And Custom Event Example

Node JS execute in 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 Node JS event loop mechanism and tell you how to create custom event in Node JS by example.

1. Node JS Event Loop Introduction.

When you execute an asynchronous method provided by Node JS framework, you should provide a callback function to the method. You can continue to execute other js code without having to wait for the method to complete.

The callback function is an observer of the event queue, when the task complete, Node JS framework will add the event back to the event queue, then the callback function which just observe the event in the queue will be invoked to process the result.

Node JS Event Loop And Custom Event Example

An example of this design pattern is Node JS implemented http web server. The http web server start and listen for request. When a request arrive, it will create an event in the queue and then waiting for next request.

When the previous request process complete, the callback function will be invoked to send the response back to client. This pattern is very high performance because web server just waiting for request and do not do other things like IO operation which will cost time and resource. You can read 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 custom event and bind custom event process function with the event follow below steps.

  1. Include Node JS events built-in module.var events = require('events');
  2. Create a EventEmitter object use above module.var event_emitter = new events.EventEmitter();
  3. Create a jаvascript function as callback function which will be triggered when custom event happen.var data_receive_handler = function(){ console.log('Data received.');}
  4. Register custom event with the callback function. The callback function will observe the custom event.event_emitter.on('receive_data', data_receive_handler);
  5. 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");

Execute above js in node.

$ node custom-event.js

Below is the example output.

Connect success.Data received.Code exit
  • 48