title: Nodejs, keep socket io connection alive in MongoStore
body: <p>In a nodejs-express app, in the server.js file I set the socket-io connection.
  It works fine doing something like this</p>
  
  <pre><code>var server = require('http').createServer(app)
  var io = require('socket.io').listen(server);
  io.sockets.on('connection', function (socket) {
  
      // how can I save globally this 'socket'?
  
  });
  </code></pre>
  
  <p>I would like to save this 'socket' globally in the server.js file and so, be able to use it everywhere in the project. Like this:</p>
  
  <pre><code>app.get('/on', function(req, res){
      socket.on('test', function (data) {
          console.log(data);
      });
  });
  app.get('/emit', function(req, res){
      socket.emit('test', "ciao");
  });
  </code></pre>
  
  <p>I red there is a way to do it saving the 'socket' connection in session. My sessions settings:</p>
  
  <pre><code>app.configure(function(){
      // ....
      app.use(express.cookieParser(SITE_SECRET));
      app.use(express.session({
          secret : 
          ,store  : new MongoStore({
          mongoose_connection : mongoose.connection
          ,db: mongoose.connections[0].db
      })
      ,cookie: { maxAge: new Date(Date.now() + (1000*60*60*24*30*12)) }
      }));
       ...
  });
  </code></pre>
  
  <p>What is a good way to do it?  </p>
  
  <p>And also, after have made this saving, how to use socket.on() and socket.emit() loading the first page if the connection is not opened yet?</p>