A really silly-novice question about events

101 views
Skip to first unread message

q2dg2b

unread,
Feb 5, 2014, 6:45:47 PM2/5/14
to nod...@googlegroups.com
Hello friends

I don't know why this code doesn't work:

var source = new (require("events").EventEmitter)()
var sink = new (require("events").EventEmitter)()
setInterval(function() {source.emit("radiation")}, 1000)
sink.on("radiation", function(){console.log("¡Detected!")})

However, this code does work:

var source = new (require("events").EventEmitter)()
setInterval(function() {source.emit("radiation")}, 1000)
source.on("radiation", function(){console.log("¡Detected!")})

Isn't possible to emit an event by an object and to receive this event by another object, then?

Thanks!!

zladuric

unread,
Feb 6, 2014, 4:42:27 AM2/6/14
to nod...@googlegroups.com
The keyword here is 'on' method. source.on vs sink.on. On is eventemitter method that listens to events emited on THAT object.

So if you emit stuff from source, attaching a listener to sink does not help. It doesn't matter what kind of object your sink is - it did not emit any radiation.

To make one object emit events and have others listen, you have two simple routes. One is to get attached to source from all your listeners, or the other is to use some kind of a bus or pubsub.

For the first version, your source would be an EventEmitter and it would emit('radiation'). And your sink can be any other object (including an event emiter), which has a listener.

Something like this:
var Sink =  new (require("events").EventEmitter)() /* or whatever else */
Sink.radiationHandler = function(){
    console.log('run to the hills!');
}
var sink = new Sink;

Then you attach that method as a listener to it, like this:

source.on('radiation', sink.radiationHandler);

The other method I've described is a pubsub, kind of like this:

Angel Java Lopez

unread,
Feb 6, 2014, 4:56:41 AM2/6/14
to nod...@googlegroups.com
The line

sink.on("radiation", function(){console.log("¡Detected!")})

says: "when sink object emit radiation event, do something"

But your event emitter is the source object.

So, to take an event from source, and act on sink, you must write something like

source.on("radiation", function () { sink.dosomething() });

Angel "Java" Lopez
@ajlopez



--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Gagle

unread,
Feb 6, 2014, 5:15:11 AM2/6/14
to nod...@googlegroups.com


El jueves, 6 de febrero de 2014 00:45:47 UTC+1, q2dg2b escribió:
Hello friends


Isn't possible to emit an event by an object and to receive this event by another object, then?

Thanks!!

Nope

Angel Java Lopez

unread,
Feb 6, 2014, 5:31:37 AM2/6/14
to nod...@googlegroups.com
A refinement

source.on("radiation", function (dataradiation) { sink.process(dataradiation) });

could be written as

source.on("radiation", sink.process);

q2dg2b

unread,
Feb 6, 2014, 3:58:15 PM2/6/14
to nod...@googlegroups.com
Ook! Thanks a lot!!

q2dg2b .

unread,
Feb 9, 2014, 3:13:33 PM2/9/14
to nod...@googlegroups.com
Ahá...very interesting!! Thanks a lot!!


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
 
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/lMpuhZmcysY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages