title: Multiple Sessions in node.js
body: <p>I am making my first app in node.js,express.js,express-session.js and angular.js. 
  I have made a simple login feature in it. A user logs in and can see all his online friends. </p>
  
  <p>The problem that i am facing is that i open one window in chrome and the other in firefox. login from different accounts but friends are shown(in both browser windows) who has the latest login. How to handle this problem? </p>
  
  <p>Here is my server.js file</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var app = require("express")();
  var session = require('express-session');
  var mysql = require("mysql");
  var bodyParser = require('body-parser');
  var http = require('http').Server(app);
  var io = require("socket.io")(http);
  
  
  //initialize the session
  app.use(session({
      secret: "online",
      resave: true,
      saveUninitialized: true
  }));
  var session_data;
  
  
  app.use(require("express").static('data'));
  
  app.use(bodyParser.urlencoded({
    extended: true
  }));
  app.use(bodyParser.json());
  
  /* Creating MySQL connection.*/
  var con    =    mysql.createPool({
        connectionLimit   :   100,
        host              :   'localhost',
        user              :   'root',
        password          :   '',
        database          :   'testDbChat'
  });
  
  
  app.get("/",function(req,res){
      res.sendFile(__dirname + '/data/messages.html');
  });
  
  
  /*  Socket connectes ur machine to server */
  
  
      io.on('connection',function(socket){
          socket.on('update_list',function(data){
              //data.purpose;
              console.log(data);
  
              if((String(data.purpose.trim()))=='list friends'){
                  var query="update members set online = ? where id = ?";
                  con.query(String(query),['Y',data.id],function(err,rows){
                      // var query="select * from user where id !='"+data.id+"'";
                      var query="SELECT  members.FirstName,members.LasName,members.ID,friends.FriendsTwoID,friends.FriendOneID FROM friends JOIN members ON members.ID = friends.FriendsTwoID OR members.ID = friends.FriendOneID WHERE (friends.FriendOneID = '"+data.id+"' OR friends.FriendsTwoID = '"+data.id+"') AND (friends.`Status` = 'Confirmed' AND members.ID != '"+data.id+"')";
                      con.query(String(query),function(err,rows){
  
                          io.emit('logout update',JSON.stringify(rows));
                      });
                  });
              }
              else{
                  var query="update members set online = ? where id = ?";
                  con.query(String(query),['N',data.id],function(err,rows){
                      //var query="select * from user where id !='"+data.id+"'";
                      var query="SELECT members.FirstName,friends.FriendsTwoID From friends JOIN members ON members.ID = friends.FriendsTwoID  where friends.FriendOneID ='"+data.id+"' AND friends.Status = 'Confirmed' ";
                      con.query(String(query),function(err,rows){
                          io.emit('logout update',JSON.stringify(rows));
                      });
                  });
              }
          });
      });
  
  
  
  
  
  
  app.post('/get_list', function (req, res) {
    var query="select * from friends";
    con.query(String(query),function(err,rows){
        res.write(JSON.stringify(rows));
        res.end();
    });
  });
  
  
  
  app.post('/login', function (req, res) {
      session_data=req.session;
      console.log(req.session); // depricated
      data = {
          name:req.body.name,
          password:req.body.password
      };
      
      console.log(data);
      session_data.password=data.password;
      session_data.name=data.name;
      var obj={};
      var query="select * from members where Username = '"+data.name+"' and Password='"+data.password+"'";
      con.query(String(query),function(err,rows){
  
          if(rows.length &gt; 0){
  
              console.log(rows[0].ID);
              var un=new Buffer(String(rows[0].FirstName)).toString('base64');
              var ui=new Buffer(String(rows[0].ID)).toString('base64');
              obj.path_name="/messages.html#?un="+un+"&amp;ui="+ui;
              res.write(JSON.stringify(obj));
              res.end();
          }else{
  
              obj.path_name="invalid";
              res.write(JSON.stringify(obj)); // writes the response but has to be sent by response.end
              res.end();
          }
      });
  });
  
  app.get('/messages', function (req, res) {
      session_data=req.session;
      if(session_data.name){
          res.sendFile(__dirname + '/data/messages.html');
      }else{
          res.redirect('/');
      }
  });
  
  
  app.post('/logout', function (req, res) {
      var query="update members set online = ? where id = ?";
      con.query(String(query),['N',req.body.id],function(err,rows){});
      req.session.destroy(function(err){
          res.end();
      });
  });
  
  app.get('/home', function (req, res) {
      session_data=req.session;
      console.log(session_data);
      if(session_data.name){
          res.sendFile(__dirname + '/data/messages.html');
      }else{
          res.redirect('/');
      }
  });
  
  http.listen(3000,function(){
      console.log("Listening on 3000");
  });</code></pre>
  </div>
  </div>
  </p>