title: node.js + socket.io broadcast from server, rather than from a specific client?
body: <p>I'm building a simple system like a realtime news feed, using node.js + socket.io.</p>
  
  <p>Since this is a "read-only" system, clients connect and receive data, but clients never actually send any data of their own.  The server generates the messages that needs to be sent to all clients, no client generates any messages; yet I do need to broadcast.</p>
  
  <p>The <a href="http://socket.io/#how-to-use">documentation for socket.io's broadcast</a> (end of page) says</p>
  
  <blockquote>
    <p>To broadcast, simply add a <code>broadcast</code> flag to <code>emit</code> and <code>send</code> method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.</p>
  </blockquote>
  
  <p>So I currently capture the most recent client to connect, into a variable, then <code>emit()</code> to that socket <em>and</em> <code>broadcast.emit()</code> to that socket, such that this new client gets the new data <em>and</em> all the other clients.  But it feels like the client's role here is nothing more than a workaround for what I thought socket.io already supported.</p>
  
  <p>Is there a way to send data to all clients based on an <em>event initiated by the server</em>?</p>
  
  <p>My current approach is roughly:</p>
  
  <pre><code>var socket;
  
  io.sockets.on("connection", function (s) {
    socket = s;
  });
  
  /* bunch of real logic, yadda yadda ... */
  
  myServerSideNewsFeed.onNewEntry(function (msg) {
    socket.emit("msg", { "msg" : msg });
    socket.broadcast.emit("msg", { "msg" : msg });
  });
  </code></pre>
  
  <p>Basically the events that cause data to require sending to the client are all server-side, not client-side.</p>