title: Broadcast updated data to all clients connected to nodejs server over socket.io
body: <p>I have an application that I'm writing that needs to be able to update all connected clients whenever a client connects to a local socket server via an ajax request and updates the system. handling the request is fine but sending the response from the local socket server to socket.io to broadcast to everyone is where I'm having an issue. I'm sure it's something simple I'm looking over here but this is very new to me so I'm having problems, specifically with the asynchronous programming mindset. Below is a shortened version of what I'm trying to accomplish and where I'm faltering.</p>
  
  <pre><code>var express = require('express'),
      http    = require('http'),
      net     = require('net'),
      app     = express(),
      server  = http.createServer(app),
      io      = require('socket.io').listen(server);
  
  app.get("/execute", function (req, res) {
      // connect to local socket server
      var localSock = net.createConnection("10000","127.0.0.1");
      localSock.setEncoding('utf8');
  
      localSock.on('data', function(data) {
          // send returned results from local socket server to all clients
          do stuff here to data ...
          send data to all connected clients view socketio socket...
          var dataToSend = data;
          localSock.end();
  
      }).on('connect', function(data) {
          // send GET data to local socket server to execute
          var command = req.query["command"];
          localSock.write(command);   
  });
  
  app.get (..., function() {});
  
  app.get (..., function() {});
  
  server.listen('3000');
  
  io.on('connection', function(client) {
      client.broadcast.send(dataToSend);
  });
  </code></pre>