Every article I read about node EventEmitters talks about how to create them. However, I have not seen a concrete example of why to use them instead of just a simple function. So for example , this is an example in a book I am reading of how to use the EventEmitter class on custom object via its constructor.
var util = require('util');
var events = require('events');
var AudioDevice = {
play: function(track) {
// Stub: Trigger playback through iTunes, mpg123, etc.
console.log("playing song: " + track);
},
stop: function() {
console.log("song stopped");
}
};
function MusicPlayer() {
this.playing = false;
events.EventEmitter.call(this);
}
util.inherits(MusicPlayer, events.EventEmitter);
var musicPlayer = new MusicPlayer();
musicPlayer.on('play', function(track) {
this.playing = true;
AudioDevice.play(track);
});
musicPlayer.on('stop', function() {
this.playing = false;
AudioDevice.stop();
});
musicPlayer.emit('play', 'The Roots - The Fire');
setTimeout(function() {
musicPlayer.emit('stop');
}, 1000);
However, the following gives me the same result:
var AudioDevice = {
play: function(track) {
// Stub: Trigger playback through iTunes, mpg123, etc.
console.log("playing song: " + track);
},
stop: function() {
console.log("song stopped");
}
};
function createMusicPlayer() {
var musicPlayer = {};
musicPlayer.playing = false;
musicPlayer.play = function(track) {
musicPlayer.playing = true;
AudioDevice.play(track);
},
musicPlayer.stop = function(track) {
musicPlayer.playing = false;
AudioDevice.stop();
}
return musicPlayer
}
var musicPlayer = createMusicPlayer();
musicPlayer.play('The Roots - The Fire');
setTimeout(function() {
musicPlayer.stop();
}, 1000);
I'm wondering if event emitters are a design choice or a necessity when working with node. I know its a necessity to understand them since many modules employ this pattern, but I am curious if that choice is analogous to using factories over constructors etc. In other words is there anything I can do with EventEmitters that I can't do with functions ?
Event-Driven Programmingjs uses events heavily and it is also one of the reasons why Node. js is pretty fast compared to other similar technologies. As soon as Node starts its server, it simply initiates its variables, declares functions and then simply waits for the event to occur.
Conclusion. Event emitters and listeners are crucial to NodeJS development, and many other programming languages development. They are very useful when you have some function that needs to execute “whenever this other thing happens”, without requiring that function to finish or even work for that matter.
EventEmitter
s are meant for implementing Publish-subscribe pattern. The idea here is that publisher - in your example it's MusicPlayer
- doesn't know or care who subscribes to his messages. His job is simply emitting proper events and whoever listens to them will receive proper notification about the event.
Implementing publish-subscribe pattern can mitigate coupling between areas of the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With