I am trying to make an event fire in my node JS code but cannot work out why it isn't.
I have a module as follows:
var zookeeper = require('node-zookeeper-client'),
events = require('events'),
util = require('util');
var client;
var KafkaZookeeperMonitor = function(){
events.EventEmitter.call(this);
}
util.inherits(KafkaZookeeperMonitor, events.EventEmitter);
KafkaZookeeperMonitor.prototype.connectToZk = function(zookeepers){
client = zookeeper.createClient(zookeepers);
client.connect();
client.once('connected', function () {
console.log('Connected to ZooKeeper');
this.emit('connected');
});
}
module.exports = KafkaZookeeperMonitor;
and I call this from my index.js file as follows:
var KafkaZookeeperMonitor = require('./kafkaZookeeperMonitor'),
kafkaStatus = new KafkaZookeeperMonitor(),
brokerPath = '/brokers/ids';
kafkaStatus.connectToZk('192.168.50.252:2181');
kafkaStatus.once('connected', function(){
console.log('watcher connected to Zookeeper');
})
When I run the code I get the log message
'Connected to Zookeeper'
but the this.emit('connected');
does not seem to fire.
I've looked around on the web and I think I'm setting up my event emitter correctly. Can anybody help me out where I might be going wrong here?
Thanks