title: Node.js + Socket.io + Redis app via PM2 with large memory footprint
body: <p>I'm new to both node.js and socket.io, but I'm trying to build a simple service that listens to Redis notifications (fed by PHP app), and broadcasts them to any users currently logged in, connected to a socket.io room e.g. 'site_name:user:user_id'.</p>
  
  <p>I have it working, but the memory footprint of the Node app quickly gets larger and larger, going from 100mb to 200+mb pretty quickly with about 100 users online and actively browsing, and I'm wondering if I have something set up wrong here. </p>
  
  <p>PM2 is handling the node app, and nginx is set up as reverse proxy.</p>
  
  <p>Server side:</p>
  
  <pre><code>var app = require('express')();
  var server = require('http').Server(app);
  var io = require('socket.io')(server);
  var redis = require('redis');
  var redisClient = redis.createClient();
  var allClients = [];
  
  server.listen(8890);
  
  io.sockets.on('connection', function (socket) {
      allClients.push(socket);
  
      socket.on('subscribe', function(data) {
          console.log('Joining room', data.room);
          socket.join(data.room);
          redisClient.subscribe(data.room);
      })
  
      socket.on('disconnect', function() {
          console.log('Disconnect');
          var i = allClients.indexOf(socket);
          delete allClients[i];
      });
  
  });
  
  // Watch for connection errors and log
  redisClient.on('error', function (err) {
      console.log('Error event - ' + redisClient.host + ':' + redisClient.port + ' - ' + err);
  });
  
  redisClient.on('message', function(room, message) {
      console.log('New message: ' + message + '. In room: ' + room);
      io.sockets.in(room).emit('message', message);
  });
  </code></pre>
  
  <p>Client side:</p>
  
  <pre><code>// connect to socket
  socket = io.connect('http://localhost:8890');
  
  // subscribe to user's room once connected
  socket.on('connect', function(data){
      socket.emit('subscribe', { room: site_name + ':user:' + user_id });
  });
  
  // show messages from user's "room" via redis notifications
  socket.on('message', function (response) {
      var json = new Hash(JSON.decode(response, true) || {});
      roar.alert(json.title, json.message, { link: json.link });
  });
  </code></pre>
  
  <p>Seems like this should be a very lean app, no? What's a normal memory footprint for a simple node.js app?</p>
  
  <p>The server starts up at 41mb, but even without anybody connecting to it, memory creeps up slowly, about 1mb a minute. Once I start connecting users, it bloats up quickly to 200+mb until I kill it.</p>
  
  <p>I'm not clear on how best to handle the redisClient and socket connections as users connect &amp; disconnect, and I thought that might be the issue. But seeing it creep up while idle is disconcerting.</p>
  
  <ul>
  <li>PM2 v0.15.7</li>
  <li>node v0.12.7</li>
  <li>socket.io v1.3.7</li>
  <li>express v4.13.3</li>
  <li>nginx v1.6.2</li>
  </ul>
  
  <p>Any help much appreciated.</p>