title: Express session and redirect inside socket.io event
body: <p>I'm new to NodeJs and i'm trying to find a way to define an express session variable on socket.io events, but dunno if it's possible.</p>
  
  <p>Would like to do these two action, but inside a socket.io event</p>
  
  <pre><code>req.session.user = data.login;
  res.redirect('/');
  </code></pre>
  
  <p>My purpose is : When the user logs successfully, to redirect him and add a session variable named 'user' in order to reach the main page.  </p>
  
  <pre><code>app.get('/', function(req,res){
      if (req.session.user) {
          res.render('accueil.html',{});
      } else {
          res.redirect('/connection');
      }
  });
  </code></pre>
  
  <p>Here is my client side code :</p>
  
  <pre><code>var express = require('express');
  var swig = require('swig');
  var ent = require('ent');
  var bodyParser = require('body-parser');
  var mongoose = require('mongoose');
  var session = require('express-session');
  
  var app = express();
  var server = app.listen(3000);
  var io = require('socket.io').listen(server);
  
  // Environnement
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({ extended: true }));
  app.use("/public", express.static(__dirname + "/public"));
  app.engine('html', swig.renderFile);
  app.set('view engine', 'html');
  app.set('views', __dirname + '/views');
  app.set('view cache', false);
  swig.setDefaults({ cache: false });
  app.use(session({
      secret: 'poussixthetruefighteroftheparadise',
      saveUninitialized: true,
      resave: true
  }));
  
  //MongoDB
  var db = mongoose.connect('mongodb://127.0.0.1:27017/maBase');
  var userSchema = new mongoose.Schema({
      username:  String,
      mail: String,
      password: String,
      friends: [],
      friends_demand: [],
      default_sound: String
  });
  var user = db.model('user', userSchema);
  
  
  
  // Page Principale
  app.get('/', function(req,res){
      if (req.session.user) {
          res.render('accueil.html',{});
      } else {
          res.redirect('/connection');
      }
  });
  
  
  // Page connection
  app.get('/connection', function(req,res){
      res.render('index.html',{});
  });
  
  
  io.on('connection', function(socket){
  
      socket.on('sub_data', function(data){
          if (data.pass1.length &gt; 6) {
              if (data.pass1 == data.pass2) {
                  user.find({$or: [{username: data.username}, {mail: data.email}]}).limit(1).exec(function(err,users){
                      if (!users.length) {
                                  var values = new user({
                                      username: ent.encode(data.username),
                                      mail: ent.encode(data.email),
                                      password: ent.encode(data.pass1)
                                  });
                                  values.save(function(){socket.emit('sub_instruction',{message: "k"});});
                      } else {
                          if (users[0].mail == data.email) {
                              socket.emit('sub_instruction', {message: "Cette adresse mail a déja été utilisé."});
                          } else { socket.emit('sub_instruction', {message: "Ce nom d'utilisateur existe déja."});}
                      }
                  });
              } else {socket.emit('sub_instruction', {message: "Les mots de passes doivent être identiques."});}
          } else {socket.emit('sub_instruction', {message: "Le mot de passe doit contenir au minimum 7 caractères."});} 
      });
  
      socket.on('con_data', function(data){
          user.find({$or: [{username: data.login, password: data.pass},{mail: data.login, password: data.pass}]}).limit(1).exec(function(err, users){
              if (!users.length) {
                  socket.emit('con_instruction', {message: "Login ou mot de passe incorrect."});
              }
              else {
                  req.session.user = data.login;
                  res.redirect('/');
              }
          });
      });
  });
  </code></pre>