title: Use same socket.io server for two different node apps.
body: <p>New to node with problems with socket.io, trying to connect two different aps to the same socket.io server and have this: </p>
  
  <p>webindex.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var app = express();
  var server = require('http').createServer(app);
  var io = require('socket.io')(server);
  
  server.listen(8080);
  
  io.on('connection', function(client) {
    client.on('color', function() {
      console.log("color server");
      client.emit('calor');
    });
  });
  
  app.set('view engine', 'ejs')
  
  app.use(express.static(__dirname + '/public'));
  
  app.get('/', function(req, res) {
    res.render('index_web.ejs');
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>mobileindex.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var express = require('express');
  var app = express();
  var server = require('http').createServer(app);
  var io = require('socket.io')(server);
  var MongoClient = require('mongodb').MongoClient;
  var ObjectId = require('mongodb').ObjectId;
  var db;
  
  server.listen(3000);
  
  app.set('view engine', 'ejs')
  
  MongoClient.connect('mongodb://charlie:12345678@ds023674.mlab.com:23674/shapes', function(err,database){
    if(err) return console.log(err);
    db = database;
  });
  
  app.use(express.static(__dirname + '/public'));
  
  app.get('/', function (req, res) {
    db.collection('shapes').find().toArray(function(err, result){
      if (err) return console.log(err)
      res.render('index.ejs', {shapes: result})
    })});
  
  app.get('/shapes/:id', function(req,res){
    db.collection('shapes').find({"_id": ObjectId(req.params.id)}).toArray(function(err,result){
      res.send(JSON.stringify(result[0]));
    });
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>index.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var socket = io.connect('http://localhost:8080');
  
  socket.on('color', function(){
    console.log('color');
    sortShapes('color');
  });
  
  socket.on('shape', function(){
    console.log('shape');
    sortShapes("name");
  });
  
  
  function sortShapes(by){
  
    var myArray = $("img");
  
    console.log(myArray);
  
    myArray.sort(function (a, b) {
  
        a = $(a).attr(by);
        b = $(b).attr(by);
  
        if(a &gt; b) {
            return 1;
        } else if(a &lt; b) {
            return -1;
        } else {
            return 0;
        }
    });
  
    $("#original").append(myArray);
  }
  
  $(function() {
  
    $("img").mouseover(function(){
      $.ajax({
        type: "GET",
        url: "http://localhost:3000/shapes/" + $(this).attr("mongo_id"),
        success: displayShapeInfo,
        error: function(){
          console.log("Error!")
        },
        dataType:"json"
      });
    });
  
    function displayShapeInfo(shape){
      $('#shape_info').text("type: " + shape.name + " color: " + shape.color + " times seen: " + shape.times_seen);
      addTimesSeen(shape._id);
    }
  
    function addTimesSeen(id){
      $.ajax({
        type: "PUT",
        url: "http://localhost:3000/shapes/" + id,
      });
    }
  
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>index_web.js</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var socket = io.connec('http://localhost:8080');
  
  $(function(){
  
    $('#color').on('click', function(){
      console.log("color");
      socket.emit('color');
    })
  
    $('#shape').on('click', function(){
      console.log("shapes");
      socket.emit('shape');
    })
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Why my socket.on in index.js are not working properly? </p>
  
  <p>Im sure the events are triggered correctly. </p>