6

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 ?

Bartosz Gościński
  • 1,468
  • 1
  • 16
  • 28
William
  • 4,422
  • 17
  • 55
  • 108

1 Answers1

11

EventEmitters 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.

Bartosz Gościński
  • 1,468
  • 1
  • 16
  • 28
  • Thanks, I see. So it's like Android's eventbus concept. – Woppi Jul 29 '18 at 10:13
  • But doesn't a regular function also not care who it's sending a response to? – Matt123 Dec 15 '20 at 20:28
  • 2
    That's right. The difference is that in case of regular functions the caller is the initiator. One can say that the caller "pulls" the message (returned value) from the function. In case of pubsub pattern the idea is that the caller is notified whenever new message is ready, not necessarily upon subscription (emitter "pushes" messages to subscribers). Also in case of regular function call there is only ever one value returned per invocation. Event emitter can emit given message 0, 1 or multiple (even infite) times – Bartosz Gościński Dec 16 '20 at 10:42