title: Emulate server in Node.js - stream delimited file with 1-second pauses
body: <p>I'm new to Node.js and I am writing a client to consume a text-based TCP stream from a server.  For testing purposes, I want to simulate the server in Node so I can test with no other dependencies.</p>
  
  <p>I have a file of captured data that looks like:</p>
  
  <pre><code>$X,... &lt;-- broadcast every second 
  $A,...
  $A,...
  $B,...
  $X,... &lt;-- broadcast every second 
  $A,...
  $A,...
  $C,...
  $X,...  &lt;-- broadcast every second 
  </code></pre>
  
  <p>The server emits a line starting with $X every second.  The other records are broadcast as events happen.  How can I modify my network server below to broadcast this data and throttle it so it emits one line at a time and pauses for one second every time it encounters a line starting with $X?</p>
  
  <p>Here is my code so far which reads in the data and broadcasts it over a port:</p>
  
  <pre><code> var http = require('http')
   , fs = require('fs')
  ;
  var server = http.createServer(function (req, res) {
   var stream = fs.createReadStream(__dirname + '/data.txt');
   stream.pipe(res);
  });
  server.listen(8000);
  console.log('server running on 8000');
  </code></pre>
  
  <p>This works but obviously just streams out the whole file at warp speed.  What I want is to spit out all of the lines from one $X to the next, pause for one second (close enough for testing purposes) and then continue to the next $X and so on like:</p>
  
  <pre><code>&gt; telnet 127.0.0.1 8000
  $X,... 
  $A,...
  $A,...
  $B,...
  
  (output would pause for one second)
  
  $X,...
  $A,...
  $A,...
  $C,...
  
  (output would pause for one second)
  
  $X,...
  ...
  </code></pre>
  
  <p>In my example above, the broadcast always starts from the beginning of data.txt when I connect with a client.  Ideally, this server would keep broadcasting this data in a loop, allowing clients to disconnect and reconnect at any time and start receiving data wherever the server simulator was currently at.   </p>
  
  <p>(PS - data.txt is a relatively small file, &lt; 1MB in most cases)</p>
  
  <p><strong>UPDATE</strong> - </p>
  
  <p>Thanks to Laurent's pointer, I was able to get it working with the following:</p>
  
  <pre><code>var net = require('net'),
       fs = require('fs'),
       async = require('async');
  
  var server = net.createServer(function (socket) {
    var lines = fs.readFileSync(__dirname + '/data-small.txt').toString().split(/\n+/);
  
    async.whilst(
      function () {
        return lines.length &gt; 0;
      },
      function (done) {
          var line = lines.shift();
          socket.write(line + '\r\n');
          setTimeout(done, /^\$X,/.test(line) ? 1000 : 0);
        },
      function (err) {
        // no more lines present
        socket.end();
      });
  });
  
  server.listen(8000);
  console.log('server running on 8000');
  </code></pre>
  
  <p>I'm now getting a blast of lines until an $X, a 1s pause, and then it continues!  Thanks!  </p>
  
  <p>Drilling into my 2nd part: is there a way to synchronize output of this faux server so all clients see the same output regardless of when they connect? </p>