Skip to content

Instantly share code, notes, and snippets.

@oscarrenalias
Created September 10, 2012 18:47
Show Gist options
  • Select an option

  • Save oscarrenalias/3692870 to your computer and use it in GitHub Desktop.

Select an option

Save oscarrenalias/3692870 to your computer and use it in GitHub Desktop.
An example of an event emitter class in Node.js
var events = require('events'),
util = require('util');
EventGenerator = function() {
events.EventEmitter.call(this);
this.read = function() {
var data = "this is new data";
this.emit("data", data);
}
}
util.inherits(EventGenerator, events.EventEmitter);
function setup() {
return(new EventGenerator());
}
var generator = setup();
generator.on("data", function(data) {
console.log("new data received: " + data);
})
generator.read();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment