title: SocketIO Presence System Redis
body: <p>I'm building a presence system for live streaming using Socket.IO with Redis, with load balanced instances.
  Everytime the user enters a room, I send to the server a socket.emit('join', roomId) and the server joins the user to the corresponding room.
  Every minute, I refresh my dashboard and get all the users connected to the instance by using the redis adapter like this:</p>
  <pre><code>io.of('/').adapter.clients([req.params.name], (err, clients) =&gt; {
          console.log(clients.length); // an array containing all connected socket ids
          if (req.query.detail) {
              res.send(clients);
          } else {
              res.send({onlineUsers: clients.length});
          }
      });
  </code></pre>
  <p>If one of my servers goes down, Redis automatically removes all the connected sockets and refreshes almost instantaneously the number of sockets in that room.</p>
  <p>Now the problem:
  Whenever the user connects, I call</p>
  <pre><code>io.of('/').on('connection', function (socket) {
      //.. set user Online in database
  });
  </code></pre>
  <p>for setting the user online.</p>
  <p>And when the user disconnects I do</p>
  <pre><code>io.of('/').on('disconnect', function (socket) {
      //... Set user offline
  });
  </code></pre>
  <p>The problem is...
  If my server goes down for a while, the user will be marked as Online forever.
  Seen that I have more than 20k people online, it's almost impossible to send every minute a request to the server update the database with a LastSeen, because this would cause more than 300 writes/sec (I don't think mongodb will handle, will it ?)</p>
  <p>Is there any how to solve this problem? Use redis to store some data in case the server goes offline for a while, I recover the last state ? How could that happen?</p>
  <p>Is there any solution for this ? Even if socketio retries to reconnect, what if the server goes offline for 1 minute and the user leaves, he'll be online forever.</p>
  <p>THANK YOU !</p>