title: Node.js WebSocket Broadcast
body: <p>I'm using the <a href="https://github.com/ncr/node.ws.js" rel="nofollow">ws library</a> for WebSockets in Node.js and
  I'm trying this example from the library examples:</p>
  
  <pre><code>var sys = require("sys"),
      ws = require("./ws");
  
    ws.createServer(function (websocket) {
      websocket.addListener("connect", function (resource) { 
        // emitted after handshake
        sys.debug("connect: " + resource);
  
        // server closes connection after 10s, will also get "close" event
        setTimeout(websocket.end, 10 * 1000); 
      }).addListener("data", function (data) { 
        // handle incoming data
        sys.debug(data);
  
        // send data to client
        websocket.write("Thanks!");
      }).addListener("close", function () { 
        // emitted when server or client closes connection
        sys.debug("close");
      });
    }).listen(8080);
  </code></pre>
  
  <p>All OK. It works, but running 3 clients, for instance, and sending "Hello!" from one will make the server only reply "Thanks!" to the client which sent the message, not to all.</p>
  
  <p>How can I broadcast "Thanks!" to all connected clients when someone sends "Hello!"?</p>
  
  <p>Thanks!</p>