18

A typical Redis chat example will go something like this (see https://github.com/emrahayanoglu/Socket.io-Redis-RealTime-Chat-Example/blob/master/chatServer.js for just one such example):

io.sockets.on('connection', function (client) { //websocket connection

  redis1.subscribe("chat");

  redis1.on("message", function(channel, message) {
      console.log(message);
      client.send(message);
  });

  client.on('message', function(msg) {
    redis2.publish("chat",msg.message);  
  });

  client.on('disconnect', function() {
      redis1.quit();
  });
});

However, the issue here is that when a client 'disconnect', the redis1.on('message',FUNC(){}) listener is still attached. The console will continue to print out the message. If one were to inspect the event listners of redis1, they would still find the anonymous function listening.

The issue is that there is no redis1.off(...) function. So, how does one unbind/unsubscribe/delete/remove the redis message listener?

Note: One cannot just do redis1.end(), since that will break the redis connection for other websocket connected users.

hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41

1 Answers1

27

The only solution that I've found by playing around in the node REPL is to not use the redis.on() function to subscribe. Instead, one should use the redis.addListener() and redis.removeListener() functions. In addition, one must not use anonymous functions as event callbacks. One could do something like this:

var callback = function(channel, message){

};

redis1.addListener('message', callback);

client.on('disconnect', function(){
  redis1.removeListener('message', callback);    
})
hrdwdmrbl
  • 4,814
  • 2
  • 32
  • 41
  • 5
    The redis client is an [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter), which has an `on` method which is just an alias for `addListener`, and `removeListener`, which is it's opposite. So you can keep using `on`, while still using `removeListener`. – Linus Thiel Jul 24 '12 at 14:03
  • 14
    For anyone like me that just skimmed this and missed the point: the removeListener is actually removing the FUNCTION, not just clearing that binding.. I.E. `removeListener('message', callback)` is removing the 'callback' function from the 'message' binding. Meaning if you're using an anonymous function `redis1.addListener('message', function(c,m){ .. }`, then you have no way of removing the listener and redis will continue to publish to dead sockets – TheRightChoyce Aug 13 '13 at 20:18
  • @Thechoyce Your comment gives me more information than the answer! – laike9m Sep 07 '14 at 17:14