---
title: "EventEmitter in Nodejs"
description: "Sometime back had pawed at Making promises with javascript but node school of thought offers an attractive pattern of in JS."
url: "https://h3manth.com/scribe/eventemitter-nodejs/"
canonical_url: "https://h3manth.com/scribe/eventemitter-nodejs/"
markdown_url: "https://h3manth.com/scribe/blog/eventemitter-nodejs.md"
publishDate: "2012-12-16T00:00:00.000Z"
author: "Hemanth HM"
category: "javascript"
tags: ["nodejs","javascript","web"]
word_count: 159
reading_time_minutes: 1
x_markdown_tokens: 421
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# EventEmitter in Nodejs

> Sometime back had pawed at Making promises with javascript but node school of thought offers an attractive pattern of in JS.

Sometime back had pawed at [Making promises with javascript](http://h3manth.com/content/making-promise-javascript) but node school of thought offers an attractive pattern of EventEmitters. 

EventEmitters are very much similar to PubSub, but is more tightly coupled and gives more control.

I shall try to demonstrate these EventEmiters with few simple code snippets.

// Let's make a child ;) var child = new (require('events').EventEmitter);

// Child's response child.once("born",function(){console.log("Say : Hello World!");}); child.on("cry",function(){console.log("The child is crying!");}); child.on("laugh",function(){console.log("Awe that is cute :)");});

//By default listener limit is 10, it can be increased using: child.setMaxListeners(100);

// Let the events emmit. child.emit("born"); child.emit("cry"); child.emit("laugh");

if(child.emit("born")){ // This wont execute console.log("Born again!"); } // Because birth is an one time event. console.log(child.emit("born"));

Output : ` Say : Hello World! The child is crying! Awe that is cute :) false `

On a serious note, we can use this in a real programming scenario like :

db.insert({...},function(err,Record){  
if(error) emit("insertFailed",{err:err, customer:customer});  
else emit("added",customer);  
} 

Anyway happy coding! Enjoy Emitting Events :)
