title: Node.js flush socket after write
body: <p>I'm implementing a tcp protocol in Node.</p>
  
  <p>Full source:<br>
  <a href="https://github.com/roelandmoors/ads.js/blob/master/ads.js" rel="nofollow">https://github.com/roelandmoors/ads.js/blob/master/ads.js</a></p>
  
  <p>specs:<br>
  <a href="http://infosys.beckhoff.com/content/1033/tcadsamsspec/html/tcadsamsspec_amstcppackage.htm?id=17754" rel="nofollow">http://infosys.beckhoff.com/content/1033/tcadsamsspec/html/tcadsamsspec_amstcppackage.htm?id=17754</a></p>
  
  <p>The problem is that I use this to send a package:</p>
  
  <pre><code>this.tcpClient.write(buf);
  </code></pre>
  
  <p>If I send multiple commands, then multiple commands are combined into a single tcp packet.<br>
  This doesn't work.</p>
  
  <p>There are more questions about this on SO, but they recommend using a delimeter.<br>
  But since I can't change the protocol this isn't an option.</p>
  
  <p>Isn't there a simple solution to flush the socket?</p>
  
  <p>socket.setNoDelay() doesn't help.</p>
  
  <p>Edit: I also tried to use the drain event to send the next write, but the event is never called?</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This seems to solve the problem, but is very uggly and I don't now if it always works.<br>
  Instead of writing it directly I write to a buffer:</p>
  
  <pre><code>this.writeFILO.push(buf);
  </code></pre>
  
  <p>Every cycle(?) I'm writing a package to the socket stream:</p>
  
  <pre><code>var sendCycle = function(ads) {
      if (ads.writeFILO.length &gt; 0) {
          ads.tcpClient.write(ads.writeFILO.shift());
      }
      setTimeout(function() {
          sendCycle(ads);
      }, 0);
  }
  </code></pre>