Pub Sub with Event Emitter

I’m a big fan of the Pub Sub (Publish, Subscribe) design pattern, and it’s one that I tend to reach for often. I’ve written previously about it, but that was using jQuery and was frontend specific. Today I’d like to swap and look at using this pattern …


This content originally appeared on Jack Franklin and was authored by Jack Franklin

I'm a big fan of the Pub Sub (Publish, Subscribe) design pattern, and it's one that I tend to reach for often. I've written previously about it, but that was using jQuery and was frontend specific. Today I'd like to swap and look at using this pattern in Node.js environments.

The idea behind the Pub Sub approach is that objects can publish events, and data associated with those events. Other objects (or the same one) can then subscribe to those events, and be notified when those events occur, and gain access to the data in the event too.

The reason that this is a good thing is because it keeps modules decoupled. Module A can subscribe to the events Module B publishes, and vice versa, without the other one knowing that they have. The less an individual module knows about other modules, the better.

It's nice and straight forward using Node's EventEmitter class, too:

var EventEmitter = require('events').EventEmitter;

var server = new EventEmitter();

server.on('foo', function() {
  console.log('got foo');
});

server.emit('foo');

In a system with multiple modules, I've taken the approach of passing in my EventEmitter instance when creating my modules:

var emitter = new EventEmitter();

moduleA.init({
   emitter: emitter
});

moduleB.init({
   emitter: emitter
});

That way the two can publish and subscribe to the same instance.

We can also create modules that inherit from the EventEmitter. This means that you can call EventEmitter methods like on and emit directly on your own modules:

var EventEmitter = require('events').EventEmitter;
var util = require('util');

var ModuleA = function() {
  this.init();
};

util.inherits(ModuleA, EventEmitter);

ModuleA.prototype.init = function() {
  this.on('done', function() {
    console.log('done');
  });
}

ModuleA.prototype.doSomething = function() {
  this.emit('done');
};


var foo = new ModuleA();
foo.doSomething(); // => logs 'done'

To do this, we can use Node's util.inherits, which will in this case cause ModuleA to inherit from EventEmitter. Notice we can then call this.on and this.emit from within ModuleA. This is a nice pattern to use if you've got a module that's going to be firing a lot of events. You may chose to create your own EventEmitter object that extends Node's and adds some extra shared functionality relevant to the context of your application. As an additional bonus, you can also use Browserify on this code and run it in the browser, so if you're building a front end app and would like to use EventEmitters, you can.

I highly encourage you to play with EventEmitters and the publish and subscribe pattern; once you're comfortable with it I find it's a great way to keep your code organised, decoupled and extensible with very little effort.


This content originally appeared on Jack Franklin and was authored by Jack Franklin


Print Share Comment Cite Upload Translate Updates
APA

Jack Franklin | Sciencx (2014-03-05T00:00:00+00:00) Pub Sub with Event Emitter. Retrieved from https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/

MLA
" » Pub Sub with Event Emitter." Jack Franklin | Sciencx - Wednesday March 5, 2014, https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/
HARVARD
Jack Franklin | Sciencx Wednesday March 5, 2014 » Pub Sub with Event Emitter., viewed ,<https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/>
VANCOUVER
Jack Franklin | Sciencx - » Pub Sub with Event Emitter. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/
CHICAGO
" » Pub Sub with Event Emitter." Jack Franklin | Sciencx - Accessed . https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/
IEEE
" » Pub Sub with Event Emitter." Jack Franklin | Sciencx [Online]. Available: https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/. [Accessed: ]
rf:citation
» Pub Sub with Event Emitter | Jack Franklin | Sciencx | https://www.scien.cx/2014/03/05/pub-sub-with-event-emitter/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.