Node JS Event Loop And Custom Event Example

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.

  1. 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.
  2. 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.
    node-js-event-loop-diagram
  3. 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.
  4. When the previous request process is complete, the callback function will be invoked to send the response back to the client.
  5. 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.
  6. 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.

  1. 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.
  2. Include Node JS events built-in module.
    var events = require('events');
  3. Create an EventEmitter object use the above module.
    var event_emitter = new events.EventEmitter();
  4. 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.');
    }
  5. Register custom events with the callback function. The callback function will observe the custom event.
    event_emitter.on('receive_data', data_receive_handler);
  6. Trigger custom event then the callback function will be executed.
    event_emitter.emit('receive_data');

3. Node JS Custom Event Example.

  1. 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");
  2. Run the below command to execute the above javascript source code in Node.JS.
    $ node custom-event.js
  3. Below is the above example output.
    Connect success.
    Data received.
    Code exit

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Clicky