title: Socket.io pm2 reload duplicates sockets
body: <p>I am running node.js in cluster mode with socket.io and pm2 now like this (default socket.io chat "hello world" example)</p>
  
  <pre><code>var
      probe = require('pmx').probe(),
      app = require('express')(),
      server = require('http').createServer(app),
      io = require('socket.io')(server),
  
      port = 3131 + parseInt(process.env.NODE_APP_INSTANCE),
      counter = probe.counter({
          name: 'User online'
      });
  
  
  
  io.on('connection', function(socket) {
      counter.inc();
      socket.on('disconnect', function() {
          counter.dec();
      });
  
      socket.on('message', function() {
          socket.emit('message', port);
      });
  });
  
  server.listen(port, function() {});
  </code></pre>
  
  <p>and default html client (all who visits my page, executes this)</p>
  
  <pre><code>var socket;
  window.addEventListener("load", function() {
      socket = io.connect('http://localhost:3000/', {
          'reconnection': true,
          'reconnectionDelay': 500,
          'reconnectionAttempts': 3
      });
  
      socket.on('message', function(message) {
          console.log(message);
      });
  
      setInterval(function() {
          socket.emit('message', {});
      }, 2000);
  });
  </code></pre>
  
  <p>this goes with default nginx sticky sessions configuration (<a href="http://socket.io/docs/using-multiple-nodes/" rel="nofollow noreferrer">http://socket.io/docs/using-multiple-nodes/</a> found here, one difference, i skipped last redis part, only nginx config is used)</p>
  
  <pre><code>upstream io_nodes {
      ip_hash;
      server 127.0 .0 .1: 3131;
      server 127.0 .0 .1: 3132;
      server 127.0 .0 .1: 3133;
      server 127.0 .0 .1: 3134;
      server 127.0 .0 .1: 3135;
      server 127.0 .0 .1: 3136;
      server 127.0 .0 .1: 3137;
      server 127.0 .0 .1: 3138;
  }
  </code></pre>
  
  <p>and pm2 config like this:</p>
  
  <pre><code>{
      "apps": [{
          "name": "server",
          "script": "server.js",
          "instances": 8,
          "exec_mode": "cluster",
          "max_memory_restart": "2G"
      }]
  }
  </code></pre>
  
  <p>And i have several problems:</p>
  
  <ol>
  <li><p>First is user online count is highter when whole my project online users count, for example (real case!) if i have 3800 users online, my keymetrics (or simple io.eio.clientsCount) shows 4000 or even 5000 total clients connected. </p></li>
  <li><p>Follows from the first, if i use pm2 reload or pm2 restart, i have case   when first node duplicates or even multiplies several times sockets count, and gets 100%+ cpu load. Sometimes all nodes, not only first multiplies sockets count. To avoid this i need to kill pm2, wait a minute, and when start again.</p></li>
  </ol>
  
  <p>Situation after pm2 reload (in real there is only 3868 users online): </p>
  
  <p><a href="https://i.stack.imgur.com/UlVVo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UlVVo.png" alt="in real there is only 3868 users online"></a></p>