balaphp
balaphp

Reputation: 1326

How to get event name in node.js event listener?

I am dynamically creating event listener for some events. In that event listener i want to do another emit call based on the event name, so i want to get the event listener name

I am using node.js eventemitter.

     var events     = require('events').EventEmitter;
     var util       = require('util');

       .....
       .....

     for(i in events) {
        transport.on(events[i],function(userId) {
            eventName = events[i];
            var acct = accountsList[userId];
            if(acct) {
                acct.emit(events[i],userId);
            }
        });
    }

The above method is working but the problem line is

       acct.emit(events[i],userId);

that events[i] is having last value of the loop. so if received any event it always emitting the final loop of the events[i] value...

Upvotes: 3

Views: 5730

Answers (5)

Martin Penkov
Martin Penkov

Reputation: 21

You can pass the event name through the context param. On EventEmitter3 the .on function params are (eventName, functionName, context). What helped me figure out how to get the eventName in the callback function was to pass this string as context and access it with the this keyword. Hope this helps whoever comes to this question and has something like this as an issue.

Upvotes: 0

Austin
Austin

Reputation: 1

you can use a closure like this which can be practical soln..

for(i in events) {
        transport.on(events[i],closureplusopn(events[i]))
     }

     function closureplusopn(eventName){
          return function(userID){

            var acct = accountsList[userId];
            if(acct) {
                acct.emit(eventName,userID);
            }
         }
     }

Upvotes: 0

elwarren
elwarren

Reputation: 326

The for loop you are using is async and results in all calls using the last value in events. If you replace that with forEach it will run sync. Try something like this untested code:

 events.forEach(function(i) {
    transport.on(events[i],function(userId) {
        eventName = events[i];
        var acct = accountsList[userId];
        if(acct) {
            acct.emit(events[i],userId);
        }
    });
});

Upvotes: 0

Salman
Salman

Reputation: 9447

So you are preserving value of event_name in a closure. It is legal, but doesn't look very neat.

Instead, you could use EventEmitter2 module like this

var EventEmitter = require('eventemitter2').EventEmitter2;

var emitter = new EventEmitter();

emitter.on('ev1', function (line) {  
  console.log(this.event); // here's your fired event
});

emitter.emit('ev1', 'has fired');

Check out the documentation, you could do much more than the original EventEmitter

Upvotes: 3

balaphp
balaphp

Reputation: 1326

i overcome this by function ...but i want to know this right way or not...

   for(i in events) {
     function test(event_name){
        transport.purpleEvents.on(event_name,function(userId) {
            var acct = accountsList[userId];
            if(acct) {
                acct.emit(event_name,userId);
            }
        });
      }
      test(events[i]);
    }

Upvotes: 0

Related Questions