title: NodeJS Chat Server Broadcast : Pipe vs Write
body: <p>I'm trying to make a simple chat server for friends/office which will at anytime have a maximum of 50 people connected.</p>
  
  <ul>
  <li>A user's message is broadcast to all connected users</li>
  <li>Server is based
  on 'net' (TCP/IP) (Will move into 'http' server later on)</li>
  <li>Users
  connect to server via Telnet</li>
  </ul>
  
  <p>Which of the below methods is good practice to broadcast messages in terms of efficiency and/or reliability?</p>
  
  <ul>
  <li>Pipe all users to one another
  (or)</li>
  <li>Upon receiving data, write to all users other than itself?</li>
  </ul>
  
  <p>Example Pipe Server:</p>
  
  <pre><code>    var server = net.createServer(function(socket) {
          for(var i=0; i&lt;sockets_list.length; i++){
              socket.pipe(sockets_list[i]);
              sockets_list.pipe(socket);
          }
          sockets_list.push(socket);
      });
  </code></pre>
  
  <p>Example Write Server:</p>
  
  <pre><code>    var server = net.createServer(function(socket) {
          sockets_list.push(socket);
  
          socket.on('data', function(data){
              for(var i=0; i&lt;sockets_list.length; i++){
                  if(socket != sockets_list[i]){
                      sockets_list[i].write(data);
                  }
              }
          });            
      });
  </code></pre>