SlideShare a Scribd company logo
2
Most read
8
Most read
Node.js Event Emitter
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var events = require("events"),
emitter = new events.EventEmitter(),
username = "Eyal",
password = "Vardi";
// an event listener
emitter.on("userAdded", function (username, password) {
console.log("Added user " + username);
});
// Emit an event
emitter.emit("userAdded", username, password);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var events = require("events");
var emitter = new events.EventEmitter();
emitter.once("foo", function () {
console.log("In foo handler");
});
emitter.emit("foo");
emitter.emit("foo");
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var events = require("events");
var EventEmitter = events.EventEmitter;
var emitter = new EventEmitter();
emitter.on("foo", function () { });
emitter.on("foo", function () { });
console.log( EventEmitter.listenerCount(emitter, "foo") );
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var events = require("events");
var EventEmitter = events.EventEmitter;
var emitter = new EventEmitter();
emitter.on("foo", function (){ console.log("In foo handler"); });
emitter.listeners("foo").forEach(function (handler) {
handler();
});
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var events = require("events");
var emitter = new events.EventEmitter();
emitter.on( "newListener" , function (eventName, listener) {
console.log("Added listener for " + eventName + " events");
});
emitter.on("foo", function () { });
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var EventEmitter = require("events").EventEmitter;
var util = require("util");
function UserEventEmitter() {
EventEmitter.call(this);
this.addUser = function (username, password) {
// add the user
// then emit an event
this.emit("userAdded", username, password);
};
};
util.inherits(UserEventEmitter, EventEmitter);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function SuperType(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function(){ alert(this.name); };
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = Object.create(SuperType.prototype);
SubType.prototype.sayAge = function(){
alert(this.age);
};
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
val getSuperValue
[prototype]
subVal
__proto__
val
__proto__
[prototype]
subVal
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
function SuperType(name){
this.name = name;
this.colors = ['red', 'blue', 'green'];
}
SuperType.prototype.sayName = function(){ return this.name; };
function SubType(name, age){
SuperType.call(this, name);
this.age = age;
}
SubType.prototype = Object.create(SuperType.prototype);
SubType.prototype.constructor = SubType;
SubType.prototype.sayAge = function(){
alert(this.age);
};
SubType.prototype.sayName = function(){
return SuperType.prototype.sayName.call(this) + "!!";
};
override
fix constructor
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var user = new UserEventEmitter();
var username = "colin";
var password = "password";
user.on("userAdded", function (username, password) {
console.log("Added user " + username);
});
user.addUser(username, password)
console.log(user instanceof EventEmitter);
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var fs = require("fs");
var fileName = "foo.txt";
fs.exists(fileName, function (exists) {
if (exists) { fs.stat(fileName, function (error, stats) {
if (error) { throw error; }
if (stats.isFile()) {
fs.readFile(fileName, "utf8", function (error, data) {
if (error) { throw error; }
console.log(data);
});
}
});
}
});
© 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
var EventEmitter = require("events").EventEmitter;
var util = require("util");
var fs = require("fs");
function FileReader(fileName) {
var _self = this;
EventEmitter.call(_self);
_self.on("stats", function() {
fs.stat(fileName, function(error, stats) {
if (!error && stats.isFile()) {
_self.emit("read");
}
});
});
_self.on("read", function() {
fs.readFile(fileName, "utf8", function(error, data) {
if (!error && data) { console.log(data); }
});
});
fs.exists(fileName, function(exists) {
if (exists) { _self.emit("stats"); }
});
}
util.inherits(FileReader, EventEmitter);
var reader = new FileReader("foo.txt");
Node.js Event Emitter

More Related Content

PPTX
Node.js File system & Streams
PPTX
Node.js Express
PDF
The Rust Programming Language: an Overview
PPT
Rust Programming Language
PDF
#살아있다 #자프링외길12년차 #코프링2개월생존기
DOCX
network programing lab file ,
PDF
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
PPTX
File system node js
Node.js File system & Streams
Node.js Express
The Rust Programming Language: an Overview
Rust Programming Language
#살아있다 #자프링외길12년차 #코프링2개월생존기
network programing lab file ,
The Secret Life of a Bug Bounty Hunter – Frans Rosén @ Security Fest 2016
File system node js

What's hot (20)

PDF
XSS Magic tricks
PDF
Sécurité des Applications WEB -LEVEL1
PPT
Threads V4
PDF
Exploiting Deserialization Vulnerabilities in Java
PDF
Support distributed computing and caching avec hazelcast
PDF
NodeJS for Beginner
PDF
Support Web Services SOAP et RESTful Mr YOUSSFI
PDF
Introduction to RxJS
PDF
How to Avoid Common Mistakes When Using Reactor Netty
PDF
OAuth2 and Spring Security
PPT
Jsp/Servlet
PPTX
JavaScript Promises
PDF
An introduction to Rust: the modern programming language to develop safe and ...
PDF
Goroutineと channelから はじめるgo言語
PDF
Why rust?
PDF
Angular and The Case for RxJS
PPTX
Web application security
PPTX
Swagger - make your API accessible
PPTX
Spring boot - an introduction
PPTX
Express JS
XSS Magic tricks
Sécurité des Applications WEB -LEVEL1
Threads V4
Exploiting Deserialization Vulnerabilities in Java
Support distributed computing and caching avec hazelcast
NodeJS for Beginner
Support Web Services SOAP et RESTful Mr YOUSSFI
Introduction to RxJS
How to Avoid Common Mistakes When Using Reactor Netty
OAuth2 and Spring Security
Jsp/Servlet
JavaScript Promises
An introduction to Rust: the modern programming language to develop safe and ...
Goroutineと channelから はじめるgo言語
Why rust?
Angular and The Case for RxJS
Web application security
Swagger - make your API accessible
Spring boot - an introduction
Express JS
Ad

Viewers also liked (20)

PDF
API Design and WebSocket
PPTX
Node.js Spplication Scaling
PPTX
Node.js Socket.IO
PPTX
Node js overview
PPTX
Modules and injector
PPTX
Angular 2.0 Views
PPTX
Angular 2.0 forms
PPTX
Async & Parallel in JavaScript
PPTX
Angular 2.0 Pipes
PPTX
Http Communication in Angular 2.0
PPTX
Angular 2.0 Routing and Navigation
PPTX
Routing And Navigation
PPTX
Angular 2 NgModule
PPTX
Upgrading from Angular 1.x to Angular 2.x
PPTX
Template syntax in Angular 2.0
PPTX
Component lifecycle hooks in Angular 2.0
PPTX
Angular 2.0 Dependency injection
PPTX
Angular 2 - Ahead of-time Compilation
PPTX
Performance Optimization In Angular 2
PPTX
Angular 1.x vs. Angular 2.x
API Design and WebSocket
Node.js Spplication Scaling
Node.js Socket.IO
Node js overview
Modules and injector
Angular 2.0 Views
Angular 2.0 forms
Async & Parallel in JavaScript
Angular 2.0 Pipes
Http Communication in Angular 2.0
Angular 2.0 Routing and Navigation
Routing And Navigation
Angular 2 NgModule
Upgrading from Angular 1.x to Angular 2.x
Template syntax in Angular 2.0
Component lifecycle hooks in Angular 2.0
Angular 2.0 Dependency injection
Angular 2 - Ahead of-time Compilation
Performance Optimization In Angular 2
Angular 1.x vs. Angular 2.x
Ad

Similar to Node.js Event Emitter (20)

PPTX
Objects & Classes in ECMAScript 6.0
PPTX
What’s new in ECMAScript 6.0
PDF
Steam Learn: Javascript and OOP
PDF
JavaScript patterns
PPT
Typescript - why it's awesome
PPTX
Type script by Howard
PPTX
TypeScript by Howard
PPTX
Howard type script
PPTX
OOP in JavaScript
PDF
Design Patterns in the 21st Century - Samir Talwar
PPTX
Tech Talks - Fundamentos JavaScript
PDF
Unveiling Design Patterns: A Visual Guide with UML Diagrams
PDF
Design patterns in the 21st Century
PDF
DesignPatternScard.pdf
PDF
Say It With Javascript
PDF
運用Closure Compiler 打造高品質的JavaScript
KEY
CoffeeScript - A Rubyist's Love Affair
PPTX
Metaworks3
KEY
Async. and Realtime Geo Applications with Node.js
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Objects & Classes in ECMAScript 6.0
What’s new in ECMAScript 6.0
Steam Learn: Javascript and OOP
JavaScript patterns
Typescript - why it's awesome
Type script by Howard
TypeScript by Howard
Howard type script
OOP in JavaScript
Design Patterns in the 21st Century - Samir Talwar
Tech Talks - Fundamentos JavaScript
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Design patterns in the 21st Century
DesignPatternScard.pdf
Say It With Javascript
運用Closure Compiler 打造高品質的JavaScript
CoffeeScript - A Rubyist's Love Affair
Metaworks3
Async. and Realtime Geo Applications with Node.js
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf

More from Eyal Vardi (11)

PPTX
Why magic
PPTX
Smart Contract
PDF
Rachel's grandmother's recipes
PPTX
Angular 2 Architecture (Bucharest 26/10/2016)
PPTX
Angular 2 Architecture
PPTX
Modules in ECMAScript 6.0
PPTX
Proxies in ECMAScript 6.0
PPTX
Iterators & Generators in ECMAScript 6.0
PPTX
Symbols in ECMAScript 6.0
PPTX
Scope & Functions in ECMAScript 6.0
PPTX
AngularJS Internal
Why magic
Smart Contract
Rachel's grandmother's recipes
Angular 2 Architecture (Bucharest 26/10/2016)
Angular 2 Architecture
Modules in ECMAScript 6.0
Proxies in ECMAScript 6.0
Iterators & Generators in ECMAScript 6.0
Symbols in ECMAScript 6.0
Scope & Functions in ECMAScript 6.0
AngularJS Internal

Recently uploaded (20)

PDF
DNT Brochure 2025 – ISV Solutions @ D365
PDF
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
PDF
Topaz Photo AI Crack New Download (Latest 2025)
PPTX
Introduction to Windows Operating System
PDF
MCP Security Tutorial - Beginner to Advanced
PDF
Cost to Outsource Software Development in 2025
PDF
Types of Token_ From Utility to Security.pdf
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
Cybersecurity: Protecting the Digital World
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Designing Intelligence for the Shop Floor.pdf
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
Wondershare Recoverit Full Crack New Version (Latest 2025)
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PDF
Website Design Services for Small Businesses.pdf
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
Time Tracking Features That Teams and Organizations Actually Need
PPTX
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
DNT Brochure 2025 – ISV Solutions @ D365
Ableton Live Suite for MacOS Crack Full Download (Latest 2025)
Topaz Photo AI Crack New Download (Latest 2025)
Introduction to Windows Operating System
MCP Security Tutorial - Beginner to Advanced
Cost to Outsource Software Development in 2025
Types of Token_ From Utility to Security.pdf
Weekly report ppt - harsh dattuprasad patel.pptx
Cybersecurity: Protecting the Digital World
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Designing Intelligence for the Shop Floor.pdf
Why Generative AI is the Future of Content, Code & Creativity?
Wondershare Recoverit Full Crack New Version (Latest 2025)
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Website Design Services for Small Businesses.pdf
iTop VPN Crack Latest Version Full Key 2025
Time Tracking Features That Teams and Organizations Actually Need
Embracing Complexity in Serverless! GOTO Serverless Bengaluru
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
How to Use SharePoint as an ISO-Compliant Document Management System

Node.js Event Emitter

  • 2. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var events = require("events"), emitter = new events.EventEmitter(), username = "Eyal", password = "Vardi"; // an event listener emitter.on("userAdded", function (username, password) { console.log("Added user " + username); }); // Emit an event emitter.emit("userAdded", username, password);
  • 3. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var events = require("events"); var emitter = new events.EventEmitter(); emitter.once("foo", function () { console.log("In foo handler"); }); emitter.emit("foo"); emitter.emit("foo");
  • 4. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var events = require("events"); var EventEmitter = events.EventEmitter; var emitter = new EventEmitter(); emitter.on("foo", function () { }); emitter.on("foo", function () { }); console.log( EventEmitter.listenerCount(emitter, "foo") );
  • 5. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var events = require("events"); var EventEmitter = events.EventEmitter; var emitter = new EventEmitter(); emitter.on("foo", function (){ console.log("In foo handler"); }); emitter.listeners("foo").forEach(function (handler) { handler(); });
  • 6. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var events = require("events"); var emitter = new events.EventEmitter(); emitter.on( "newListener" , function (eventName, listener) { console.log("Added listener for " + eventName + " events"); }); emitter.on("foo", function () { });
  • 7. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com
  • 8. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var EventEmitter = require("events").EventEmitter; var util = require("util"); function UserEventEmitter() { EventEmitter.call(this); this.addUser = function (username, password) { // add the user // then emit an event this.emit("userAdded", username, password); }; }; util.inherits(UserEventEmitter, EventEmitter);
  • 9. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function SuperType(name){ this.name = name; this.colors = ['red', 'blue', 'green']; } SuperType.prototype.sayName = function(){ alert(this.name); }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } SubType.prototype = Object.create(SuperType.prototype); SubType.prototype.sayAge = function(){ alert(this.age); };
  • 10. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com val getSuperValue [prototype] subVal __proto__ val __proto__ [prototype] subVal
  • 11. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com function SuperType(name){ this.name = name; this.colors = ['red', 'blue', 'green']; } SuperType.prototype.sayName = function(){ return this.name; }; function SubType(name, age){ SuperType.call(this, name); this.age = age; } SubType.prototype = Object.create(SuperType.prototype); SubType.prototype.constructor = SubType; SubType.prototype.sayAge = function(){ alert(this.age); }; SubType.prototype.sayName = function(){ return SuperType.prototype.sayName.call(this) + "!!"; }; override fix constructor
  • 12. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var user = new UserEventEmitter(); var username = "colin"; var password = "password"; user.on("userAdded", function (username, password) { console.log("Added user " + username); }); user.addUser(username, password) console.log(user instanceof EventEmitter);
  • 13. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var fs = require("fs"); var fileName = "foo.txt"; fs.exists(fileName, function (exists) { if (exists) { fs.stat(fileName, function (error, stats) { if (error) { throw error; } if (stats.isFile()) { fs.readFile(fileName, "utf8", function (error, data) { if (error) { throw error; } console.log(data); }); } }); } });
  • 14. © 2014 All rights reserved. Tel: 054-5-767-300, Email: evardi@gmail.com var EventEmitter = require("events").EventEmitter; var util = require("util"); var fs = require("fs"); function FileReader(fileName) { var _self = this; EventEmitter.call(_self); _self.on("stats", function() { fs.stat(fileName, function(error, stats) { if (!error && stats.isFile()) { _self.emit("read"); } }); }); _self.on("read", function() { fs.readFile(fileName, "utf8", function(error, data) { if (!error && data) { console.log(data); } }); }); fs.exists(fileName, function(exists) { if (exists) { _self.emit("stats"); } }); } util.inherits(FileReader, EventEmitter); var reader = new FileReader("foo.txt");