var geoip = require('geoip-lite');
var uuid = require('node-uuid');
var possible_id_characters = "ABCDEFGHJKMNPQRSTUVWXYZ23456789";

var generate_chat_id = function(){
  var text = "";
  for( var i=0; i < 10; i++ )
    text += possible_id_characters.charAt(Math.floor(Math.random() * possible_id_characters.length));

  return text;
};

var chats = {};

module.exports = function(io){
  io.on("connection", function(socket){
    socket.on("chat_id", function(data){
      // Generate a chat id if necessary and send it to the client
      var chat_id = data.chat_id;
      if(chat_id.length != 10) {
        chat_id = generate_chat_id();
      }
      if(chats[chat_id]) {
        chats[chat_id].push(socket);
      } else {
        chats[chat_id] = [socket];
      }
      socket.emit("assign", {"chat_id": chat_id});
      
      //Announce the new chat client to their chat_id
      var new_connection_geo = geoip.lookup(socket.handshake.address.address);
      var new_connection_geo_text = "Unknown Location";
      if(new_connection_geo){
        new_connection_geo_text = new_connection_geo.country;
        if(new_connection_geo.city != undefined && new_connection_geo.city.length != 0)
          new_connection_geo_text += "-" + new_connection_geo.city;
      }
      var connection_message_uuid = uuid.v4();
      for(i in chats[chat_id]){
        chats[chat_id][i].emit("receive_system", {"message_uuid": connection_message_uuid, "payload": "Connection from " + socket.handshake.address.address + " (" + new_connection_geo_text + ")"});
      }

      socket.on("send", function (data) {
        var message_uuid = uuid.v4();
        socket.emit("acknowledge", {"client_message_id": data.client_message_id, "message_uuid": message_uuid});
        for(i in chats[chat_id]){
          if(chats[chat_id][i].id != socket.id){
            chats[chat_id][i].emit("receive", {"message_uuid": message_uuid, "payload": data.message});
          }
        }
      });
    });
  });
};

@@ -689,16 +689,24 @@
 hat_id%5D.
+sockets.
 push(soc
@@ -750,16 +750,28 @@
 at_id%5D =
+ %7B%22sockets%22:
  %5Bsocket
@@ -771,16 +771,17 @@
 %5Bsocket%5D
+%7D
 ;%0A      
@@ -1353,32 +1353,40 @@
 n chats%5Bchat_id%5D
+.sockets
 )%7B%0A        chats
@@ -1386,32 +1386,40 @@
   chats%5Bchat_id%5D
+.sockets
 %5Bi%5D.emit(%22receiv
@@ -1462,32 +1462,207 @@
 on_message_uuid,
+%0A                                                          %22previous_message_uuid%22: chats%5Bchat_id%5D.last_message_uuid,%0A                                                         
  %22payload%22: %22Con
@@ -1749,24 +1749,92 @@
 xt + %22)%22%7D);%0A
+        chats%5Bchat_id%5D.last_message_uuid = connection_message_uuid;%0A
       %7D%0A%0A   
@@ -1999,32 +1999,91 @@
 %22message_uuid%22: 
+message_uuid, %22previous_message_uuid%22: chats%5Bchat_id%5D.last_
 message_uuid%7D);%0A
@@ -2113,16 +2113,24 @@
 chat_id%5D
+.sockets
 )%7B%0A     
@@ -2147,24 +2147,32 @@
 ats%5Bchat_id%5D
+.sockets
 %5Bi%5D.id != so
@@ -2207,16 +2207,24 @@
 chat_id%5D
+.sockets
 %5Bi%5D.emit
@@ -2269,57 +2269,283 @@
 uid,
- %22payload%22: data.message%7D);%0A          %7D%0A        %7D
+%0A                                                       %22previous_message_uuid%22: chats%5Bchat_id%5D.last_message_uuid,%0A                                                       %22payload%22: data.message%7D);%0A          %7D%0A        %7D%0A        chats%5Bchat_id%5D.last_message_uuid = message_uuid;
 %0A   

Send and store a message chain to the client