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:

https://gist.github.com/fatihacet/1290216


On Thursday, February 6, 2014 12:45:47 AM UTC+1, q2dg2b wrote:
>
> 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!!
>

-- 
-- 
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to