var io = require('socket.io').listen(1337);
var basket = {};

io.sockets.on('connection', function (socket) {
  socket.on("register", function(data) {
    basket[data.from] = socket.id;
      console.log('following user registered: ', data.from);
  });
  socket.on("privmessage", function(data){
    var to = basket[data.to];
    io.sockets.socket(to).emit('newmessage', { message: data.msg });
      console.log('message: ', data.msg, 'sent to: ', data.to);
  });
  socket.on("share_url", function(data){
    var to = basket[data.to];
    io.sockets.socket(to).emit('newmessage', { from: data.from, url: data.url });
      console.log('message: ', data.url, 'sent to: ', data.to);
  });
});
