title: simple social network using node.js and mongodb
body: <p>I am trying to build simple social network and I am following this book(Building Node Applications with MongoDB and Backbone)(<a href="https://github.com/Swiftam/book-node-mongodb-backbone/tree/master/ch10" rel="nofollow">https://github.com/Swiftam/book-node-mongodb-backbone/tree/master/ch10</a>). However, I just realized that the node.js version has been updated.</p>
  
  <p>I tied to solve some the issue however I got problem in chat.js that states this is the error:</p>
  
  <blockquote>
  ch10/routes/chat.js:27
   data.sessionStore.load(data.sessionID, function(err, session) {
  TypeError: Cannot read property 'load' of undefined
  </blockquote>
  
  <pre><code>module.exports = function(app, models) {
  
  var io = require('socket.io');
  var utils = require('connect').utils;
  var cookie = require('cookie');
      this.io = io;
  
  //var Session = require('connect').middleware.session.Session;
  
  var sio = io.listen(app.server);
  
  sio.configure(function() {
  
      // Utility methods to see if the account is online
      app.isAccountOnline = function(accountId) {
          var clients = sio.sockets.clients(accountId);
          return (clients.length &gt; 0);
      };
  
      sio.set('authorization', function(data, accept) {
  
          var signedCookies = cookie.parse(data.headers.cookie);
      //  var cookies = utils.parseSignedCookies(signedCookies, app.sessionSecret);
  
      //  data.sessionID = cookies['express.sid'];
          data.sessionStore = app.sessionStore;
          data.sessionStore.load(data.sessionID, function(err, session) {
           if (err || !session) {
            accept("Error", false);
               } else {
             data.session = session;
             accept(null, true);
               }
          });
      });
  
      sio.sockets.on('connection', function(socket) {
          var session = socket.handshake.session;
          var accountId = session.accountId;
          var sAccount = null;
          socket.join(accountId);
                      io.use(function (socket, next) { next(); });
          // Immediately trigger the login event
          // of this account
          app.triggerEvent('event:' + accountId, {
              from: accountId,
              action: 'login'
          });
  
          var handleContactEvent = function(eventMessage) {
              socket.emit('contactEvent', eventMessage);
          };
  
          var subscribeToAccount = function(accountId) {
              var eventName = 'event:' + accountId;
              app.addEventListener(eventName, handleContactEvent);
              console.log('Subscribing to ' + eventName);
          };
  
          // Find the account contacts and subscribe
          models.Account.findById(accountId, function subscribeToFriendFeed(account) {
              var subscribedAccounts = {};
              sAccount = account;
              account.contacts.forEach(function(contact) {
                  if (!subscribedAccounts[contact.accountId]) {
                      subscribeToAccount(contact.accountId);
                      subscribedAccounts[contact.accountId] = true;
                  }
              });
  
              // Subscribed to my feed as well
              if (!subscribedAccounts[accountId]) {
                  subscribeToAccount(accountId);
              }
          });
  
          // Remove listeners if socket disconnects
          socket.on('disconnect', function() {
              sAccount.contacts.forEach(function(contact) {
                  var eventName = 'event:' + contact.accountId;
                  app.removeEventListener(eventName, handleContactEvent);
                  console.log('Unsubscribing from ' + eventName);
              });
              app.triggerEvent('event:' + accountId, {
                  from: accountId,
                  action: 'logout'
              });
          });
                        var cookieParser = require('cookie-parser')(SESSION_SECRET);    
  
              // ### Cookie parser
  
                  // Wrapper arround Express cookie parser, so we can use the same cookie parser for socket.io.
                  // Parse Cookie header and populate `socket.request.cookies` with an object keyed by the cookie names.
                  // Uses signed cookies by passing a secret string, which assigns `socket.request.secret` so it may be used by other middleware.
  
            function cookieParserWrapper (socket, next) {
              // request, response and callback
            cookieParser(socket.request, {}, next);
              }
  
  
          // Handle incoming chats from client
          socket.on('chatclient', function(data) {
              sio.sockets.in(data.to).emit('chatserver', {
                  from: accountId,
                  text: data.text
              });
          });
      });
  
  });
  
  }
  </code></pre>