What is an event emitter?

Looking through http://microjs.com , I see a lot of libraries labeled "event emitters". I like to think that I am well versed in the basics of JavaScript code, but I really don't know what an "emitter of events" is or not.

Does anyone want to enlighten me? That sounds interesting...

+55
javascript javascript-events event-handling events
Nov 18 '12 at 9:24
source share
4 answers

It fires an event that anyone can listen to. Different libraries offer different implementations for different purposes, but the main idea is to create a basis 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, to emit an event, you need to have a DOM object and cannot generate events from an arbitrary object. Here it happens that the emitter event becomes useful. Here is some pseudo-code for demonstrating user events (the same template 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"); 
+45
Nov 18 '12 at 9:32
source share

In node.js, an event can be described simply as a string with an appropriate callback. An event can be β€œemitted” (or, in other words, calling the corresponding call) several times, or you can choose only listening on the first outcome.

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 demonstrates all the basic features of EventEmitter . The on or addListener (basically a subscription method) allows you to select an event to view and a callback to call. The emit method (publishing method), on the other hand, allows you to "emit" an event that causes all callbacks registered in the event to "shoot" (called).

From source What are event emitters?

+19
Nov 18 '12 at 9:29
source share

Consider a callback function -

 function test(int a, function(){ console.log("I am call-back function"); }){ console.log("I am a parent function"); } 

Now, when the parent function is called in the event (button press or any connection, etc.), it first executes its code, and then control is transferred to the callback function. Now the event emitter is an object / method that fires the event as soon as any action occurs to pass the cntrol to the parent function. For-example-Server is an event emitter in Node.Js programming. It emits an error event as soon as the server encounters an error that passes the control to the parent function of the error. The server fires a connection event, as soon as the socket connects to the server, this event then fires the parent function getConnections, which also performs the callback function as an argument. Thus, it is really a chain that starts as something happens using an event emitter that emits an event to trigger a function.

0
Aug 16 '17 at 20:49 on
source share

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'); 
0
Aug 24 '17 at 18:18
source share



All Articles