I'm currently creating eventListeners using the following:
client.on('data', function(data){
// Lots of other functions etc..
});
What I would like to know specifically, is whether it is possible to identify this particular listener by a unique ID so that I can refer to it individually?
There may be multiple instances of this eventListener open at any time and I want to be able to remove them based on receiving a specific data event that will refer back to this ID. The issue right now is, I have no idea how to identify individual eventListeners.
Thanks!
You can always store your listener function to a variable and add or remove it like this:
var EventEmitter = require('events').EventEmitter
emitter = new EventEmitter();
var addListener = function(id){
var listener = function(){
console.log("Listener", id);
emitter.removeListener("test", listener);
}
emitter.on("test", listener);
}
addListener("a");
addListener("b");
emitter.emit("test");
// Listener a
// Listener b
With this you have a unique id by having the listener variable that refers to the listener function. In that scope you can remove the listeners based on the scope.