node-redis-pubsub icon indicating copy to clipboard operation
node-redis-pubsub copied to clipboard

Allow for on('connect'

Open iamjon opened this issue 6 years ago • 0 comments

Hi, Thank you for the awesome project. Currently your code allows for us to provide an error handler if the clients cannot connect to redis. The code here provides this functionality. if(channel === "error"){ self.errorHandler = handler; this.emitter.on("error", handler); this.receiver.on("error", handler); callback(); return; }

It would be amazing if we had similar code that would let us know when the handler has connected something akin to if(channel === "connect"){ self.connectHandler = handler; this.emitter.on("connect", handler); this.receiver.on("connect", handler); callback(); return; }

Right now my code looks like this const handler = new NRP({ url: config.redis.url }); let connected = true; function connectError(error) { if (!connectError.loggedOnce) { logger.error(error.message); connected = false; connectError.loggedOnce = true; } } // This is never called function connectSuccess() { if (!connectSuccess.loggedOnce) { logger.info('Redis Connected: ', config.redis); connectSuccess.loggedOnce = true; } } handler.on('error', connectError); handler.on('connect', connectSuccess);

Because there is no connect handler, the code has to assume the connection is good and sets the connected variable to true by default. If I was unsure of my connection and I wanted to only set the variable once connected, or if I wanted to log that a connection was made I cannot do so.

In my local env I tried adding the code above to trigger the connection handler and it worked.

Thank you, Jonathan

iamjon avatar Feb 10 '19 13:02 iamjon