Emitting Events

Node.js is event-driven. This means that streams, and many other modules emit events. You can listen for an event to trigger and write code to take any action based on that trigger. This is similar to how browser emits the "Click", "Load", "Hover" events.

PAIDLevel: Intermediate9:29 mins
Emitting Events
You do not have access to this lesson!

Enroll in the training to get access to this lesson along with hundreds of other lessons and complete courses.

Already enrolled? Login

Course content

Emitting Events

Continuing from our previous lesson In this lesson we are going to talk about an important concept called events or Emitting Events. We are going to look at what events are and they are helpful in writing applications in Node.js.

Node.js is a runtime which is event based or event driven. It emits event when certain things happen. For example If you hover over any part on the website or click anywhere or when the page loads, events are triggered. If you click anywhere, the click event is going to trigger. So when the events get triggered, in the coding programming language, we can listen for those events to trigger and do something with it. For example, when a page loads, show a pop up or change the color of the background etc.

We'll be using the same code from our previous lesson, where we talked about the streams and had a text file with some gibberish stuff there and some code in sublime text. The data and end both are an event. The data event gets triggered when the readStream has data in the pipe line. And the end event gets triggered when the stream is ended.


The word on is a very helpful word but in actuality it's called the addListener because we are actually listening for an event called data. So we can change the word on to addListener and then run the code by writing node stream hit enter and you see it printed 355 which is the time the counter read the lorem.txt file. 


The method on is an alias of addListener method. But on is faster to write. When you're listening to an event, you can also not listen to an event. Once you understand the technology, you can also remove the listener. 

Currently you can see the function we have is an anonymous so I'm going to make it a named function and call it dataCounter. So we have two listeners, dataCounter and dataPrinter. dataCounter counts the data while dataPrinter will print the data.
Also we have so much text in the loren.txt file which is going to clutter the console so we can only print the length. So we can write console.log('data chunk length: ' + data.length); to get the length.

var fs = require('fs');

var read Stream = fs.createReadStream('lorem.txt');
 readStream.setEncoding('utf8');

  readStream.addListener('data', dataCounter);
  readStream.addListener('data', dataPrinter);
 
  readStream.addListener('end', function(){ 
    console.log(counter); 
}); 

function dataCounter(datacoming){ 
counter = counter + 1; 
} 

function dataPrinter(data){ 
  console.log('data chunk length: ' + data.length);
}


Run the code by writing node stream hit enter and you see it printed 355 times the length of data chunk characters and when the end listener got triggered it printed the counter which was 355


You can have multiple listeners listening for any particular event. You can also remove the listener as well. So let's say, when the counter is 5 so if(counter === 5) we can remove the listener. So I can say write readStream.removeListener and which data we want to remove? it could be either dataCounter or dataPrinter. Let's write dataCounter for demonstration.  

function dataCoutner(datacoming){ 
  counter = counter + 1; 
  if(counter === 5){ 
    readStream.removeListener('data', dataCounter);
 }
}

Run the code by writing node stream hit enter and you see the counter goes up to 5 because dataCounter stops running. 


You can also remove all listeners, so instead of writing any particular listener you can write readStream.removeAllListeners('data');

function dataCoutner(datacoming){ 
  counter = counter + 1;
  if(counter === 5){
    readStream.removeAllListeners('data');
 }
}

Now run the code by writing node stream hit enter and you see It printed 5 times only and then the program ended.



Node js is an event driven input and output or runtime, and a lot of their modules, features and patterns in which you're going to be input output data, reading data, processing data is going to depend on how you listen for events to trigger and then you run your callback function.

I hope you enjoyed this lesson. If you have any questions, leave your comments below. I'll talk to you in the next lesson. Good bye :) 

Become a confident software developer with mentor support!
Get Started
Already have an account? Please Login