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

Creating a NodeJS project with Express Express is one of the most popular npm packages out there, it's basically a NodeJS web application framework that provides lots of functionalities and helps you to manage everything and organize your app into MVC on the back-end, and it's very easy to get started with. To use Express you first ne...
Node Express Error: Cannot find module ‘serve-favicon’ This error means you don't have the module 'serve-favicon' installed on your project, here is the full error: Error: Cannot find module 'serve-favicon' at Function.Module._resolveFilename (module.js:542:15) at Function.Module._load (module.js:472:25) at Module.require (module.js:...
Tutorial: Creating a Realtime Notification System in Angular and Nodejs Notification System Demo In this post I'm going to show you how to create a simple realtime notification system using Angular, Nodejs and It will offer a page where we will be able to create notifications and broadcast it to all the online users. I'll be making it as basic as possible...
Angular2 Tutorial: Developing a Realtime Chat App Angular 2 Chat Application Hi guys, today we're going to make an awesome chat application with Angular2, NodeJS and, this tutorial will involve quite a lot of things, and there are also lots of small details, so it will be a little bit longer than usual. To easily understand and make t...

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>