title: ExpressJS & Websocket & session sharing
body: <p>I'm trying to make a chat application based on Node.js. I'd like to force websocket server (ws library) to using ExpressJS session system. Unfortunately, I've got stuck. MemoryStore hashes used to get sessions' data are different than session IDs in cookies. Could somebody explain me what I'm doing wrong?</p>
  
  <p>Websocket server code part:</p>
  
  <pre><code>module.exports = function(server, clients, express, store) {
    server.on('connection', function(websocket) {
      var username;
  
      function broadcast(msg, from) {...}
  
      function handleMessage(msg) {...}
  
      express.cookieParser()(websocket.upgradeReq, null, function(err) {
          var sessionID = websocket.upgradeReq.cookies['sid'];
  
              //I see same value in Firebug
          console.log(sessionID);
  
              //Shows all hashes in store
              //They're shorter than sessionID! Why?
          for(var i in store.sessions)
              console.log(i);
  
          store.get(sessionID, function(err, session) {
                  websocket.on('message', handleMessage);
  
                  //other code - won't be executed until sessionID in store
  
                  websocket.on('close', function() {...});
          });
      });
  });
  }
  </code></pre>
  
  <p>store object definition:</p>
  
  <pre><code>var store = new express.session.MemoryStore({
      reapInterval: 60000 * 10
  });
  </code></pre>
  
  <p>app configuration:</p>
  
  <pre><code>app.configure(function() {
      app.use(express.static(app.get("staticPath")));
      app.use(express.bodyParser());
      app.use(express.cookieParser());
  
      app.use(express.session({
          store: store,
          secret: "dO_ob",
          key: "sid"
      }));
  });
  </code></pre>
  
  <p>Part of main code:</p>
  
  <pre><code>var app = express();
  var httpServer = http.createServer(app);
  var websocketServer = new websocket.Server({server: httpServer});
  httpServer.listen(80);
  </code></pre>
  
  <p>Sample debugging output:</p>
  
  <pre><code>- websocket.upgradeReq.headers.cookie "sid=s%3A64a%2F6DZ4Mab8H5Q9MTKujmcw.U8PJJIR%2BOgONY57mZ1KtSPx6XSfcn%2FQPZ%2FfkGwELkmM"
  - websocket.upgradeReq.cookies["sid"] "s:64a/6DZ4Mab8H5Q9MTKujmcw.U8PJJIR+OgONY57mZ1KtSPx6XSfcn/QPZ/fkGwELkmM"
  - i "64a/6DZ4Mab8H5Q9MTKujmcw"
  </code></pre>