title: Node.js (Express JS 3.x with Socket.io together)
body: <p>I need to share session between sockets and express js.
  I've tried to make this example work: <a href="http://www.danielbaulig.de/socket-ioexpress/" rel="nofollow">http://www.danielbaulig.de/socket-ioexpress/</a> without success. Web browser return </p>
  
  <blockquote>
    <p>GET localhost:8000/socket.io/1/?t=1354005884872 500 (Internal
    Server Error)</p>
  </blockquote>
  
  <p>on session start. I get server side error: </p>
  
  <blockquote>
    <p>warn  - handshake error Error</p>
  </blockquote>
  
  <p><strong>Server side script:</strong></p>
  
  <pre><code>var express = require('express')
    , util = require('util')
    , connect = require('express/node_modules/connect')
    , parseCookie = connect.utils.parseCookie
    , MemoryStore = connect.middleware.session.MemoryStore
    , store;
  
    var app = express()
    , http = require('http')
    , server = http.createServer(app)
    , io = require('socket.io').listen(server);
  
    var connect = require('express/node_modules/connect')
    , util = require('util')
    , cookie = require('cookie')
    , parseCookie = cookie.parse
    , MemoryStore = connect.middleware.session.MemoryStore
    , store;
  
  app.configure(function () {
      app.use(express.cookieParser());
      app.use(express.session({
        secret: 'secret'
      , key: 'express.sid'
      , store: store = new MemoryStore()
      }));
      app.use(function (req, res) {
          res.send('Hello, your session id is ' + req.sessionID);
      });
  });
  
  io.set('authorization', function (data, accept) {
    if (!data.headers.cookie) 
      return accept('No cookie transmitted.', false);
  
    data.cookie = parseCookie(data.headers.cookie);
    data.sessionID = data.cookie['express.sid'];
  
    store.load(data.sessionID, function (err, session) {
      if (err || !session) return accept('Error', false);
  
      data.session = session;
      return accept(null, true);
    });
  }).sockets.on('connection', function (socket) {
    var sess = socket.handshake.session;
    socket.log.info(
        'a socket with sessionID'
      , socket.handshake.sessionID
      , 'connected'
    );
    socket.on('set value', function (val) {
      sess.reload(function () {
        sess.value = val;
        sess.touch().save();
      });
    });
  });
  
  app.use(count);
  
  // custom middleware
  function count(req, res) {
    req.session.count = req.session.count || 0;
    var n = req.session.count++;
    res.send('viewed ' + n + ' times\n');
  }
  
  server.listen(8000);
  </code></pre>
  
  <p><strong>Client side script:</strong></p>
  
  <pre><code>&lt;%= javascript_include_tag "http://localhost:8000/socket.io/socket.io.js" %&gt;
  var socket = io.connect('http://localhost:8000');
  </code></pre>
  
  <p>socket.io v0.9.11, express.js v3.0.3, connect v2.7.0, cookie v0.0.5</p>
  
  <p>I wish you can help me.</p>