title: Why use redis in a chat application?
body: <p>I just recently built a chat, it's working pretty well, but I think I need to hook it up to redis.</p>
  
  <p>From what I understand I need redis for scaling and holding some data if a client refreshes or a server goes down.</p>
  
  <p>A core component of the 1on1 chat is that I store the users, and associate a <code>socket.id</code> to those users</p>
  
  <pre><code>var users = {};
  io.sockets.on('connection', function (socket) {
  
    // store the users &amp; socket.id into objects
    users[socket.handshake.headers.user.username] = socket.id;
  
  });
  </code></pre>
  
  <p>Now on the client side I can say hey I want to chat with "Jack", as long as that is a valid user then I can pass that data to the server, i.e the user name and message just to jack like so.</p>
  
  <pre><code>var chattingWith = data.nickname; // this is Jack passed from the client side
  io.to(users[chattingWith]).emit();
  </code></pre>
  
  <p>My question is, why should I use redis? What should I store in redis? How should I interact with that data?</p>
  
  <p>I am using an <code>io.adapter</code></p>
  
  <pre><code>io.adapter(redisIo({ 
    host: 'localhost', 
    port: 6379,
    pubClient: pub,
    subClient: sub
  }));
  </code></pre>
  
  <p>Also reading code from an example app I see when a socket connects they save the socket data into redis like so.</p>
  
  <pre><code>// store stuff in redis
  redisClientPublish.sadd('sockets:for:' + userKey + ':at:' + room_id, socket.id, function(err, socketAdded) {
    if(socketAdded) {
      redisClientPublish.sadd('socketio:sockets', socket.id);
      redisClientPublish.sadd('rooms:' + room_id + ':online', userKey, function(err, userAdded) {
        if(userAdded) {
          redisClientPublish.hincrby('rooms:' + room_id + ':info', 'online', 1);
          redisClientPublish.get('users:' + userKey + ':status', function(err, status) {
            io.sockets.in(room_id).emit('new user', {
              nickname: nickname,
              provider: provider,
              status: status || 'available'
            });
          });
        }
      });
    }
  });
  </code></pre>
  
  <p>They use it when entering a room, to get information about the room. </p>
  
  <pre><code>app.get('/:id', utils.restrict, function(req, res) {
  
    console.log(redisClientPublish);
  
    utils.getRoomInfo(req, res, redisClientPublish, function(room) {
  
      console.log('Room Info: ' + room); 
  
      utils.getUsersInRoom(req, res, redisClientPublish, room, function(users) {
  
        utils.getPublicRoomsInfo(redisClientPublish, function(rooms) {
  
          utils.getUserStatus(req.user, redisClientPublish, function(status) {
            utils.enterRoom(req, res, room, users, rooms, status);
          });
  
        });
  
      });
  
    });
  
  });
  </code></pre>
  
  <p>So again, I am asking because I am kind of confused if I need to store anything inside redis/why I need to, for instance we may have a few hundred thousand users and the node.js server "Jack" and "Mike" are chatting on goes down, it then changes to point to a new node.js instance.</p>
  
  <p>Obviously I want the chat to still remember "Jack's" socket id is "12333" and "Mike's" socket id is "09278" so whenever "Jack" says hey I want to send "Mike/09278" a message the server side socket will direct it properly.</p>
  
  <p>Would storing the username as a key and socket ID as a value be a wise use case for redis, would that socket.id still work?</p>