title: Node.js and Comet
body: <p>Basically, I am trying to set up a basic comet server and client using node.js from scratch. A tried to Google things about the process it should work and as I am testing it seems to work fine. However one question is still in my head. First I would like to show you the code.</p>
  
  <pre><code>var http = require('http');
  var sys = require('sys');
  var fs = require('fs');
  var qs = require('querystring');
  
  var server = http.createServer();
  
  var connections = [];
  
  server.on('request', function(req, res) {
  
      console.log(req.url);
  
      if(req.url == '/index') {
  
          fs.readFile(__dirname + '/index.html', function(err, data){
              if(err) {
                  res.writeHead(err, data);
                  return res.end('Error loading index.html');
              }
  
              res.writeHead(200);
              res.end(data);
          });
      } else if(req.url == '/sendData') {
          if (req.method == 'POST') {
              var body = '';
              req.on('data', function (data) {
                  body += data;
              });
              req.on('end', function () {
  
                  var POST = qs.parse(body);
                  console.log(POST);
                  res.writeHead(200);
                  res.end("");
                  broadcastData(POST);
              });
          }
  
      } else {
          connections.push(res);
      }
  });
  
  function broadcastData(data) {
      for(var i = 0; i &lt; connections.length; i++) {
          connections[i].writeHead(200);
          connections[i].end(JSON.stringify({'message': data}));
      }
  }
  
  process.openStdin().addListener('data', function(chunk) {
      for(var i = 0; i &lt; connections.length; i++) {
          connections[i].writeHead(200);
          var message = chunk.toString();
          connections[i].end(JSON.stringify({'message': {'name': message}}));
      }
  });
  
  server.listen(4000);
  </code></pre>
  
  <p>the client side:</p>
  
  <pre><code>function doComet() {
      $.getJSON('/', function(events){
          doComet();
          console.log(events);
          $('#content').append(events.message.name);
      });
  }
  
  function sendData(data) {
      $.ajax({
          type: "POST",
          url: "/sendData",
          contentType: 'application/javascript; charset=UTF-8',
          data: { name: "John", location: "Boston" }
      }).done(function( msg ) {
          console.log(msg)
      });
  }
  </code></pre>
  
  <p>So there is a listener which sends the data which I write on stdin and also there's a button on the client side which will send the data to the server and the server sends it to all clients.</p>
  
  <p>My question is what if a lot of events happen in a really short amount of time? I mean as soon as a response got from the server another ajax request is sent but there should be a short amount of time when the client is not connected yet and if happens something during that time the client will not have the fresh data.</p>
  
  <p>So does my thoughts right about this? In this case somehow I should sync the data properly to make sure everybody gets it. Does anybody know how to do it properly?</p>
  
  <p>Thank you for you help!</p>