title: I'm receiving duplicate messages in my clustered node.js/socket.io/redis pub/sub application
body: <p>I'm using Node.js, Socket.io with Redisstore, Cluster from the Socket.io guys, and Redis.</p>
  
  <p>I've have a pub/sub application that works well on just one Node.js node. But, when it comes under heavy load is maxes out just one core of the server since Node.js isn't written for multi-core machines.</p>
  
  <p>As you can see below, I'm now using the Cluster module from Learnboost, the same people who make Socket.io.</p>
  
  <p>But, when I fire up 4 worker processes, each browser client that comes in and subscribes gets 4 copies of each message that is published in Redis.  If there are are three worker processes, there are three copies.</p>
  
  <p>I'm guessing I need to move the redis pub/sub functionality to the cluster.js file somehow.</p>
  
  <p><strong>Cluster.js</strong></p>
  
  <pre><code>var cluster = require('./node_modules/cluster');
  
  cluster('./app')
    .set('workers', 4)
    .use(cluster.logger('logs'))
    .use(cluster.stats())
    .use(cluster.pidfiles('pids'))
    .use(cluster.cli())
    .use(cluster.repl(8888))
    .listen(8000);
  </code></pre>
  
  <p><strong>App.js</strong></p>
  
  <pre><code>redis = require('redis'),
  sys = require('sys');
  
  var rc = redis.createClient();
  
  var path = require('path')
    , connect = require('connect')
    , app = connect.createServer(connect.static(path.join(__dirname, '../')));
  
  // require the new redis store
  var sio = require('socket.io')
    , RedisStore = sio.RedisStore
    , io = sio.listen(app);
  
  io.set('store', new RedisStore);io.sockets.on('connection', function(socket) {
      sys.log('ShowControl -- Socket connected: ' + socket.id);
  
      socket.on('channel', function(ch) {
          socket.join(ch)
          sys.log('ShowControl -- ' + socket.id + ' joined channel: ' + ch);
      });
  
      socket.on('disconnect', function() {
          console.log('ShowControll -- Socket disconnected: ' + socket.id);
      });
  });
  
  rc.psubscribe('showcontrol_*');
  
  rc.on('pmessage', function(pat, ch, msg) {
      io.sockets.in(ch).emit('show_event', msg);
      sys.log('ShowControl -- Publish sent to channel: ' + ch);
  });
  
  // cluster compatiblity
  if (!module.parent) {
    app.listen(process.argv[2] || 8081);
    console.log('Listening on ', app.address());
  } else {
    module.exports = app;
  }
  </code></pre>
  
  <p><strong>client.html</strong></p>
  
  <pre><code>&lt;script src="http://localhost:8000/socket.io/socket.io.js"&gt;&lt;/script&gt;
  &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      var socket = io.connect('localhost:8000');
      socket.emit('channel', 'showcontrol_106');
      socket.on('show_event', function (msg) {
          console.log(msg);
          $("body").append('&lt;br/&gt;' + msg);
      });
  &lt;/script&gt;
  </code></pre>