Nodejs Totorial: Creating and Using Events

Nodejs makes it very easy for us to use events, and we’re not limited to a collection of predefined events, it has a built-in module that allows us create and emit our own events.

Let me show you how easy it is to use this module, first we have require the events module and create the event emitter:

var ev = require('events');
var emitter = new ev.EventEmitter();

Ok, now we’re ready to start creating the events. As an example I’m going to use an event that I named new user, let’s create its event listener:

emitter.on('new user', function(){
   console.log('A new user has connected.');
});

I’ve used the function on from the EventEmitter to add this event listener, it receives the event name and a callback function that executes once the event is triggered.

Now the application is already listening the new user event, this is how we can emit it:

emitter.emit('new user');

Simple right? All we have to do is to call the emit function passing the event name.

But what if we have to pass parameters when emitting the event? Optionally, you can also pass some arguments to the emit function if you need, then you just have to declare them on the callback function as well. Here’s an example:

emitter.on('new user', function(user){
   console.log('The user '+user+' has connected.');
});
 
emitter.emit('new user','user1');

That’s it guys! Thanks for reading, if you have any doubts please let me know in the comments.

Recommended for you

Node.js Tutorial: Reading and Writing files Reading and writing files is a very trivial task, no matter what programming language you use you probably have already came across a situation where you had to do it, and it's not different for node.js developers, luckily node.js makes this task very easy for us, with the file system(fs) module we ...
Express.js: How to Change the Default Port By default Express uses port 3000, but sometimes this port is already been used by another application, so you can either close the other application or change the port. In this short tutorial I'm going to show you how easy it is to change the port. First let's open the bin/www file and locate th...
Creating a Live Auction App with Angular 2, Node.js and Socket.IO Angular 2 Auction App In this tutorial we're going to build a very cool Live Auction Application, my goal is to demonstrate how Angular 2, Node.js and Socket.io work together, and as everyone knows Angular 2 will soon come out so I thought it would be great to write a tutorial involving it. Of c...
How to Execute a .jar File with Node.js Child Processes You probably have already come across situations where your application had to execute other applications or native commands from the OS. Node.js can easily do that by spawning child processes. To show you how to do that I'm going to give you an example of how to execute a .jar file with node. Fi...

Leave a Reply

Your email address will not be published. Required fields are marked *

Obs: Use the tag <pre lang="LANGUAGE"> to include code blocks to your comment.
Example: <pre lang="javascript"> console.log('Test'); </pre>