/**
 * Module dependencies.
 */

var _express = require('express');
var _routes = require('./routes');
var _notifications = require('./routes/notifications');
var _http = require('http');
var _path = require('path');
var _io = require('socket.io');
var _redis = require('redis');

var app = _express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', _path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(_express.favicon());
app.use(_express.logger('dev'));
app.use(_express.json());
app.use(_express.urlencoded());
app.use(_express.methodOverride());
app.use(app.router);
app.use(_express.static(_path.join(__dirname, 'public')));

// development only
if ('development' === app.get('env')) {
  app.use(_express.errorHandler());
}

// map urls
app.get('/', _routes.index);
app.get('/notifications', _notifications.notifications);

var server = _http.createServer(app);

var io = _io.listen(server);

function xxxxxxx(socket, redisClient, redisKey, redis_err, redis_reply) {

  console.log('redisClient.get() - redis_err: "' + redis_err
      + '" - redis_reply: "' + redis_reply + '"');

  if (redis_err !== null) {
    socket.emit('internal', {
      type : 'error',
      code : 'USER_ID_RETRIEVAL_RETURNED_ERROR',
      message : 'Error detected when trying to get user id.'
    });
    return;
  }

  if (redis_reply === null) {
    socket.emit('internal', {
      type : 'error',
      code : 'USERID_IS_NULL',
      message : 'Couldn\'t get userId.'
    });
    return;
  }

  // FIXME: should use something like 'get-and-delete'
  console.log("Remoging retrieved key");
  redisClient.del(redisKey);

  var userId = redis_reply;

  redisClient.on("error", function(err) {
    // TODO: infor this error to client (using websocket)
    // TODO: close this websocket (so the client knows and reconnect)
    console.log("Error " + err);
  });

  redisClient.on('message', function(pattern, data) {
    console.log('Suscriber received a message: ' + data);
    socket.emit('notification', {
      message : data
    });
  });

  var url = '/app/user/' + userId + '/notifications';
  console.log("Subscribing to " + url);
  redisClient.subscribe(url);

  socket.emit('internal', {
    type : 'success',
    code : 'SUBSCRIPTION_OK',
    message : 'Subscription to pub/sub ok.'
  });

}

io.of('/io/user/notifications').on('connection', function(socket) {
  console.log('Connection from ' + socket);

  socket.on('subscribe-to-notifications', function(data) {
    console.log('subscribe-to-notifications - data.uuid: "' + data.uuid);

    var redisKey = 'cookie-' + data.uuid;
    var redisClient = _redis.createClient();
    redisClient.get(redisKey, function(err, reply) {
      xxxxxxx(socket, redisClient, redisKey, err, reply);
    });
  });

});

server.listen(app.get('port'), function() {
  console.log('Express server listening on port ' + app.get('port'));
});

@@ -272,16 +272,56 @@
 redis');
+%0Avar _httpProxy = require('http-proxy');
 %0A%0Avar ap
@@ -817,16 +817,498 @@
 ());%0A%7D%0A%0A
+var server = _http.createServer(app);%0Avar io = _io.listen(server);%0Avar proxy = _httpProxy.createProxyServer();%0A%0Afunction pythonProxy(req, res) %7B%0A  console.log(%22Proxying request...%22);%0A  proxy.web(req, res, %7B%0A    target : 'http://localhost:3010'%0A  %7D, function(e) %7B%0A    console.log(%22err: %22 + e);%0A    var respondeBody = 'ERROR: ' + e;%0A    res.writeHead(200, %7B%0A      'Content-Length' : respondeBody.length,%0A      'Content-Type' : 'text/plain'%0A    %7D);%0A    res.end(respondeBody);%0A  %7D);%0A%7D%0A%0A
 // map u
@@ -1401,74 +1401,38 @@
 s);%0A
-%0Avar server = _http.createServer(app);%0A%0Avar io = _io.listen(server
+app.get('/python', pythonProxy
 );%0A%0A

Add reverse proxy for /python url