What is an "event emitter"?
Solution 1:
It triggers an event to which anyone can listen. Different libraries offer different implementations and for different purposes, but the basic idea is to provide a framework for issuing events and subscribing to them.
Example from jQuery:
// Subscribe to event.
$('#foo').bind('click', function() {
alert("Click!");
});
// Emit event.
$('#foo').trigger('click');
However, with jQuery in order to emit an event you need to have a DOM object, and cannot emit events from an arbitrary object. This is where event-emitter becomes useful. Here's some pseudo-code to demo custom events (the exact same pattern as above):
// Create custom object which "inherits" from emitter. Keyword "extend" is just a pseudo-code.
var myCustomObject = {};
extend(myCustomObject , EventEmitter);
// Subscribe to event.
myCustomObject.on("somethingHappened", function() {
alert("something happened!");
});
// Emit event.
myCustomObject.emit("somethingHappened");
Solution 2:
In node.js an event can be described simply as a string with a corresponding callback. An event can be "emitted" (or in other words, the corresponding callback be called) multiple times or you can choose to only listen for the first time it is emitted.
Example:-
var example_emitter = new (require('events').EventEmitter);
example_emitter.on("test", function () { console.log("test"); });
example_emitter.on("print", function (message) { console.log(message); });
example_emitter.emit("test");
example_emitter.emit("print", "message");
example_emitter.emit("unhandled");
> var example_emitter = new (require('events').EventEmitter);
{}
> example_emitter.on("test", function () { console.log("test"); });
{ _events: { test: [Function] } }
> example_emitter.on("print", function (message) { console.log(message); });
{ _events: { test: [Function], print: [Function] } }
> example_emitter.emit("test");
test //console.log'd
true //return value
> example_emitter.emit("print", "message");
message //console.log'd
true //return value
> example_emitter.emit("unhandled");
false //return value
This demonstates all the basic functionality of an EventEmitter
. The on or addListener
method (basically the subscription method) allows you to choose the event to watch for and the callback to be called. The emit
method (the publish method), on the other hand, allows you to "emit" an event, which causes all callbacks registered to the event to 'fire', (get called).
From the source What are Event Emitters?
Solution 3:
Simple example in Node.js:
var EventEmitter = require('events').EventEmitter;
var concert = new EventEmitter;
var singer = 'Coldplay';
concert.on('start', function (singer) {
console.log(`OMG ${singer}!`);
});
concert.on('finish', function () {
console.log(`It was the best concert in my life...`);
});
concert.emit('start', singer);
concert.emit('finish');
Solution 4:
Consider a call-back function-
function test(int a, function(){
console.log("I am call-back function");
}){
console.log("I am a parent function");
}
Now, whenever the parent function is called on an event(a button click or any connection etc) , it first executes its code, and then control is passed to the call-back function. Now, an event emitter is an object/method which triggers an event as soon as some action takes place so as to pass the cntrol to the parent function. For-example- Server is an event emitter in Node.Js programming. It emits event of error as soon as the server encounters an error which passes the control to error parent function. Server emits an event of connection as soon as a socket gets connected to server, this event then triggers the parent function of getConnections, which indeed also takes a call-back function as argument. So, it indeed is a chain, which is triggered as something happens by event emitter which emits an event to start a function running.