var express = require('express')
  , app     = express()
  , http    = require('http')
  , server  = http.createServer(app)
  , io      = require('socket.io').listen(server);


var port = process.env.PORT || 5000; // Use the port that Heroku provides or default to 5000

// listen for new web clients:
server.listen(port);

app.use(express.static(__dirname + '/'));


console.log('http server listening on %d', port);

var usersSockets = {};

console.log('websocket server created');

io.sockets.on('connection', function (socket) {



    socket.on('close', function() {
        socket.broadcast.emit('close', {"fb_id" : socket.user});
        console.log('websocket connection close');
        if(socket.user != null) {
          delete usersSockets[socket.user];
        }
    });

    socket.on('disconnect', function () {
        socket.broadcast.emit('close', {"fb_id" : socket.user});
        console.log('websocket disconnect');
        if(socket.user != null) {
          delete usersSockets[socket.user];
        }
    });

    socket.on('connect', function(data) {
        socket.user = data.fb_id;
        usersSockets[socket.user] = socket;
    });

    socket.on('photo_received', function(data) {
        var receiverId    = data.fb_id
        var base64image   = data.image;
        var locationImage = data.position;

        var myUserId      = socket.user;

        var socketUser  = usersSockets[receiverId];



        socketUser.emit('photo_received',  {"image" : base64image, "fb_id" : myUserId, "position" : locationImage} ); 
    });

    socket.on('position_update', function(data) {
         console.log(data);
         if(data.latitude && data.longitude) {
           
           var socketUser = socket.user;
           
           console.log('socketUster ' + socketUser);
           if(socketUser) { 
              socket.broadcast.emit('position_update', { 
            						            "fb_id" : socket.user,
                                    "position" : {
                                    "lat" : data.latitude,
                                    "long" : data.longitude
                                 }});

           } else {
            console.log('ERROR: Problem with userssocket');
           }
         } 
     });

});

@@ -598,34 +598,36 @@
 emit('close', %7B%22
-fb
+user
 _id%22 : socket.us
@@ -863,18 +863,20 @@
 ose', %7B%22
-fb
+user
 _id%22 : s
@@ -1100,18 +1100,20 @@
  = data.
-fb
+user
 _id;%0A   
@@ -1177,30 +1177,26 @@
 ket.on('
+send_
 photo
-_received
 ', funct
@@ -1240,18 +1240,20 @@
  = data.
-fb
+user
 _id%0A    
@@ -1497,18 +1497,20 @@
 image, %22
-fb
+user
 _id%22 : m
@@ -1924,10 +1924,12 @@
    %22
-fb
+user
 _id%22

refactor name fb_id to user_id