SlideShare a Scribd company logo
1 of 23
Professional Node.js
CH5. Using the Event Emitter Pattern to Simplify Event
Binding
WHAT’S IN THIS CHAPTER?
• Introducing the event emitter pattern
• Binding and unbinding event listeners
• Creating your own event emitter
2015/2/26 2
Introducing the event emitter pattern
• In Node many objects emit events.
• a TCP server can emit a “connect” event every time a new client connects.
• a file stream can emit a “data” event every time a new chunk of data is read.
2015/2/26 3
UNDERSTANDING THE STANDARD
CALLBACK PATTERN
• Asynchronous programming does not use function return values to denote
that a function is finished. Instead it uses the continuation-passing style (CPS)
2015/2/26 4
UNDERSTANDING THE EVENT
EMITTER PATTERN
• The standard callback pattern
• It works well when you want the client to be notified when a function completes.
• The event emitter pattern
• Use a standard interface to clearly separate the event emitter and the event listener.
• Two or more objects are involved — the event emitter and one or more event listeners.
2015/2/26 5
UNDERSTANDING THE EVENT
EMITTER PATTERN (2)
2015/2/26 6
UNDERSTANDING EVENT TYPES
• emitted events always have a type, which is represented by a string.
• by convention, event types are usually lowercase words with no spaces.
• Ex. data, end
2015/2/26 7
UNDERSTANDING EVENT TYPES(2)
• Event emitter is a generic interface that serves any type of event.
• “Error” event
• It is a special case in the Node implementation.
• Most event emitter implementations in Node will emit an “error” event every time
there is an error.
• If the programmer chooses to not listen to that event type and an event with the “error”
type occurs, the event emitter will notice it and raise that error as an uncaught exception.
2015/2/26 8
UNDERSTANDING EVENT TYPES(3)
2015/2/26 9
USING THE EVENT EMITTER API
• .addListener and .on — To add an event listener to an event type
• .once — To attach an event listener to a given event type that will be called at
most once
• .removeEventListener — To remove a specifi c event listener of a given
event
• .removeAllEventListeners — To remove all event listeners of a given event
type
2015/2/26 10
Binding Callbacks Using .addListener()
or .on()
2015/2/26 11
Binding Multiple Event Listeners
• The event emitter pattern allows multiple event listeners to listen to the same
event type on the same event emitter.
2015/2/26 12
Binding Multiple Event Listeners(2)
• The event emitter is then responsible for calling all the registered listeners for
a given event type, and it will call them in the order in which they were
registered.
• An event listener might not be called immediately after the event is emitted. There
might be other event listeners called before it.
• Throwing exceptions into the stack is not normal behavior, but it might be caused by a
bug in your code. When an event is emitted, if one of the event listeners throws an
error when invoked, some event listeners might never be called. In that case, the event
emitter will catch the error and may handle it.
2015/2/26 13
Binding Multiple Event Listeners(3)
2015/2/26 14
Removing an Event Listener from an Event
Emitter Using .removeListener()
2015/2/26 15
Getting a Callback Executed at Most Once
Using .once()
2015/2/26 16
Removing All Event Listeners from an
Event Emitter Using .removeAllListeners()
2015/2/26 17
CREATING AN EVENT
EMITTER
2015/2/26 18
Inheriting from Node Event Emitter
2015/2/26 19
Emitting Events
• By creating a class that inherits from EventEmitter, instances of MyClass can
emit events:
2015/2/26 20
Summary
• The event emitter pattern is a recurrent pattern in Node.
• You can use it to decouple event emitter objects from the code interested in
certain events.
• event_emitter.on() - to register listeners for certain types of events.
• event_emitter.removeListener() - to unregister them.
• You can create your own event emitters by inheriting from the EventEmitter
class and simply using the .emit() function.
2015/2/26 21
References
• Professional Node.js – Building Javascript Based Scalable Software
• Author: Pefro Teixeira
• http://blog.caesarchi.com/2011/06/nodejs-eventemitter.html
2015/2/26 22
~ THE END ~
2015/2/26 23

More Related Content

Viewers also liked

James elastic search
James   elastic searchJames   elastic search
James elastic searchLearningTech
 
Peggy 重新認識java script
Peggy 重新認識java scriptPeggy 重新認識java script
Peggy 重新認識java scriptLearningTech
 
Html, CSS, Javascript, Jquery, Meteor應用
Html, CSS, Javascript, Jquery, Meteor應用Html, CSS, Javascript, Jquery, Meteor應用
Html, CSS, Javascript, Jquery, Meteor應用LearningTech
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejsLearningTech
 
Express Web Application Framework
Express Web Application FrameworkExpress Web Application Framework
Express Web Application FrameworkLearningTech
 
An overview of java script in 2015 (ecma script 6)
An overview of java script in 2015 (ecma script 6)An overview of java script in 2015 (ecma script 6)
An overview of java script in 2015 (ecma script 6)LearningTech
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
Operations research - an overview
Operations research -  an overviewOperations research -  an overview
Operations research - an overviewJoseph Konnully
 

Viewers also liked (16)

James elastic search
James   elastic searchJames   elastic search
James elastic search
 
Knockout
KnockoutKnockout
Knockout
 
Ken20150313
Ken20150313Ken20150313
Ken20150313
 
Ken20150320
Ken20150320Ken20150320
Ken20150320
 
20150515ken
20150515ken20150515ken
20150515ken
 
Peggy 重新認識java script
Peggy 重新認識java scriptPeggy 重新認識java script
Peggy 重新認識java script
 
Html, CSS, Javascript, Jquery, Meteor應用
Html, CSS, Javascript, Jquery, Meteor應用Html, CSS, Javascript, Jquery, Meteor應用
Html, CSS, Javascript, Jquery, Meteor應用
 
Lodash js
Lodash jsLodash js
Lodash js
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Express Web Application Framework
Express Web Application FrameworkExpress Web Application Framework
Express Web Application Framework
 
An overview of java script in 2015 (ecma script 6)
An overview of java script in 2015 (ecma script 6)An overview of java script in 2015 (ecma script 6)
An overview of java script in 2015 (ecma script 6)
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
Operations research - an overview
Operations research -  an overviewOperations research -  an overview
Operations research - an overview
 

Similar to 20141107 node js_eventemitterpattern_anney

Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Nida Ismail Shah
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Event bus for android
Event bus for androidEvent bus for android
Event bus for android丞廷 鄭
 
Event driven architecture
Event driven architectureEvent driven architecture
Event driven architectureVinod Wilson
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Elizabeth Smith
 
Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and javaTech_MX
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programmingElizabeth Smith
 
Explained: Domain events
Explained: Domain eventsExplained: Domain events
Explained: Domain eventsJoão Pires
 
[Free OpManager training] Part 4- Network fault-management & IT automation
[Free OpManager training]  Part 4- Network fault-management & IT automation[Free OpManager training]  Part 4- Network fault-management & IT automation
[Free OpManager training] Part 4- Network fault-management & IT automationManageEngine, Zoho Corporation
 
Platform Events by Tim Taylor
Platform Events by Tim TaylorPlatform Events by Tim Taylor
Platform Events by Tim TaylorChristine Smith
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IISivaSankari36
 
PlatForm Event.pptx
PlatForm Event.pptxPlatForm Event.pptx
PlatForm Event.pptxRinku Saini
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJSLilia Sfaxi
 
004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event HandlingMohammad Hossein Rimaz
 
D8 dispatcher / subscriber
D8 dispatcher / subscriberD8 dispatcher / subscriber
D8 dispatcher / subscriberjoshirohit100
 
A Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsA Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsVMware Tanzu
 
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...eNovance
 

Similar to 20141107 node js_eventemitterpattern_anney (20)

Ss2gx
Ss2gxSs2gx
Ss2gx
 
Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.Events: The Object Oriented Hook System.
Events: The Object Oriented Hook System.
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event bus for android
Event bus for androidEvent bus for android
Event bus for android
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Event driven architecture
Event driven architectureEvent driven architecture
Event driven architecture
 
Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012Event and Signal Driven Programming Zendcon 2012
Event and Signal Driven Programming Zendcon 2012
 
Java event processing model in c# and java
Java  event processing model in c# and javaJava  event processing model in c# and java
Java event processing model in c# and java
 
Event and signal driven programming
Event and signal driven programmingEvent and signal driven programming
Event and signal driven programming
 
Explained: Domain events
Explained: Domain eventsExplained: Domain events
Explained: Domain events
 
[Free OpManager training] Part 4- Network fault-management & IT automation
[Free OpManager training]  Part 4- Network fault-management & IT automation[Free OpManager training]  Part 4- Network fault-management & IT automation
[Free OpManager training] Part 4- Network fault-management & IT automation
 
Platform Events by Tim Taylor
Platform Events by Tim TaylorPlatform Events by Tim Taylor
Platform Events by Tim Taylor
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
 
PlatForm Event.pptx
PlatForm Event.pptxPlatForm Event.pptx
PlatForm Event.pptx
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
 
004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling004 - JavaFX Tutorial - Event Handling
004 - JavaFX Tutorial - Event Handling
 
D8 dispatcher / subscriber
D8 dispatcher / subscriberD8 dispatcher / subscriber
D8 dispatcher / subscriber
 
EventHandling.pptx
EventHandling.pptxEventHandling.pptx
EventHandling.pptx
 
A Deep Dive into Spring Application Events
A Deep Dive into Spring Application EventsA Deep Dive into Spring Application Events
A Deep Dive into Spring Application Events
 
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...
OpenStack in Action 4! Nick Barcet & Julien Danjou - From ceilometer to telem...
 

More from LearningTech

More from LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Expression tree
Expression treeExpression tree
Expression tree
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection & activator
Reflection & activatorReflection & activator
Reflection & activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 
Asp.net MVC DI
Asp.net MVC DIAsp.net MVC DI
Asp.net MVC DI
 
Vic weekly learning_20151127
Vic weekly learning_20151127Vic weekly learning_20151127
Vic weekly learning_20151127
 
Mocha.js
Mocha.jsMocha.js
Mocha.js
 
R language
R languageR language
R language
 
20151120 ian cocos2d js
20151120 ian cocos2d js20151120 ian cocos2d js
20151120 ian cocos2d js
 

20141107 node js_eventemitterpattern_anney

  • 1. Professional Node.js CH5. Using the Event Emitter Pattern to Simplify Event Binding
  • 2. WHAT’S IN THIS CHAPTER? • Introducing the event emitter pattern • Binding and unbinding event listeners • Creating your own event emitter 2015/2/26 2
  • 3. Introducing the event emitter pattern • In Node many objects emit events. • a TCP server can emit a “connect” event every time a new client connects. • a file stream can emit a “data” event every time a new chunk of data is read. 2015/2/26 3
  • 4. UNDERSTANDING THE STANDARD CALLBACK PATTERN • Asynchronous programming does not use function return values to denote that a function is finished. Instead it uses the continuation-passing style (CPS) 2015/2/26 4
  • 5. UNDERSTANDING THE EVENT EMITTER PATTERN • The standard callback pattern • It works well when you want the client to be notified when a function completes. • The event emitter pattern • Use a standard interface to clearly separate the event emitter and the event listener. • Two or more objects are involved — the event emitter and one or more event listeners. 2015/2/26 5
  • 6. UNDERSTANDING THE EVENT EMITTER PATTERN (2) 2015/2/26 6
  • 7. UNDERSTANDING EVENT TYPES • emitted events always have a type, which is represented by a string. • by convention, event types are usually lowercase words with no spaces. • Ex. data, end 2015/2/26 7
  • 8. UNDERSTANDING EVENT TYPES(2) • Event emitter is a generic interface that serves any type of event. • “Error” event • It is a special case in the Node implementation. • Most event emitter implementations in Node will emit an “error” event every time there is an error. • If the programmer chooses to not listen to that event type and an event with the “error” type occurs, the event emitter will notice it and raise that error as an uncaught exception. 2015/2/26 8
  • 10. USING THE EVENT EMITTER API • .addListener and .on — To add an event listener to an event type • .once — To attach an event listener to a given event type that will be called at most once • .removeEventListener — To remove a specifi c event listener of a given event • .removeAllEventListeners — To remove all event listeners of a given event type 2015/2/26 10
  • 11. Binding Callbacks Using .addListener() or .on() 2015/2/26 11
  • 12. Binding Multiple Event Listeners • The event emitter pattern allows multiple event listeners to listen to the same event type on the same event emitter. 2015/2/26 12
  • 13. Binding Multiple Event Listeners(2) • The event emitter is then responsible for calling all the registered listeners for a given event type, and it will call them in the order in which they were registered. • An event listener might not be called immediately after the event is emitted. There might be other event listeners called before it. • Throwing exceptions into the stack is not normal behavior, but it might be caused by a bug in your code. When an event is emitted, if one of the event listeners throws an error when invoked, some event listeners might never be called. In that case, the event emitter will catch the error and may handle it. 2015/2/26 13
  • 14. Binding Multiple Event Listeners(3) 2015/2/26 14
  • 15. Removing an Event Listener from an Event Emitter Using .removeListener() 2015/2/26 15
  • 16. Getting a Callback Executed at Most Once Using .once() 2015/2/26 16
  • 17. Removing All Event Listeners from an Event Emitter Using .removeAllListeners() 2015/2/26 17
  • 19. Inheriting from Node Event Emitter 2015/2/26 19
  • 20. Emitting Events • By creating a class that inherits from EventEmitter, instances of MyClass can emit events: 2015/2/26 20
  • 21. Summary • The event emitter pattern is a recurrent pattern in Node. • You can use it to decouple event emitter objects from the code interested in certain events. • event_emitter.on() - to register listeners for certain types of events. • event_emitter.removeListener() - to unregister them. • You can create your own event emitters by inheriting from the EventEmitter class and simply using the .emit() function. 2015/2/26 21
  • 22. References • Professional Node.js – Building Javascript Based Scalable Software • Author: Pefro Teixeira • http://blog.caesarchi.com/2011/06/nodejs-eventemitter.html 2015/2/26 22
  • 23. ~ THE END ~ 2015/2/26 23

Editor's Notes

  1. 使用事件發射模式,以簡化事件綁定
  2. 非同步程式不使用函數的回傳值來表示功能執行完成。相反的,它採用了延續傳遞風格 (CPS)