title: Websockets - Getting Data (Polling?)
body: <p>This may be a silly question to some who have wrapped their head around it already and maybe I just need more <a href="/questions/tagged/coffee" class="post-tag" title="show questions tagged &#39;coffee&#39;" rel="tag">coffee</a>.</p>
  
  <p><strong>Question:</strong> Whether using <a href="/questions/tagged/websockets" class="post-tag" title="show questions tagged &#39;websockets&#39;" rel="tag">websockets</a> or <a href="/questions/tagged/ajax" class="post-tag" title="show questions tagged &#39;ajax&#39;" rel="tag">ajax</a> it seems like there is still some polling happening. Is this correct?</p>
  
  <p><strong>Example</strong> <em>(not real project)</em>: I want to keep an eye on a text file. Unless I am missing something (more coffee?), aren't I still having to either <strong>a)</strong> Ask the server if there is an update, or <strong>b)</strong> Tell the page I have an update; Through either sleeping the PHP code for a set time or having a <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout" rel="nofollow">setTimeout</a> loop on the client side.</p>
  
  <p><strong>Things I do understand:</strong> I definitely see a benefit already with talking back and forth between server and page. I see that I am not sending http requests. So I see benefits.</p>
  
  <p><strong>Details:</strong> I have always just used <a href="/questions/tagged/xmlhttprequest" class="post-tag" title="show questions tagged &#39;xmlhttprequest&#39;" rel="tag">xmlhttprequest</a> so I decided to check out this whole websockets thing as from what I thought I understood, is that data is sent to the client in real time, but, like stated above, unless I am missing something or some logic here, it seems like I still have to either tell <a href="/questions/tagged/php" class="post-tag" title="show questions tagged &#39;php&#39;" rel="tag">php</a> or <a href="/questions/tagged/javascript" class="post-tag" title="show questions tagged &#39;javascript&#39;" rel="tag">javascript</a> to check in intervals for data, otherwise data is being sent in an endless loop (imagine making a call to mysql).</p>
  
  <p>Maybe my logic in my code is all kinds of bad. You are welcome to view it. From all of the examples I found, everyone seems to just run an infinite loop in PHP</p>
  
  <p><strong>PHP</strong> (Minus all of the connection jargon)</p>
  
  <pre><code>while(true) {
      // update once a second
      $this-&gt;send($client, file_get_contents('/my/file/test.txt'));
      sleep(1);
  }
  </code></pre>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>var websocket = new WebSocket( "ws://mysite.com:12345" );
  
  websocket.onmessage = function( str ) {
      console.log( str.data );
  };
  </code></pre>
  
  <p>I am just not grasping the logic on this on how I can make it real time without some sort of polling. Maybe this is how it is supposed to work.</p>
  
  <p>I do understand that if I remove the sleep from the <a href="/questions/tagged/php" class="post-tag" title="show questions tagged &#39;php&#39;" rel="tag">php</a> code things get much more real time, too much, but this seems like it would infinitely poll the file in the example above and that doesn't seem right.</p>
  
  <p><strong>Edit:</strong> To clarify, I am not specifically looking for a specific solution to watching a text file. You may have thought this if you skimmed the question.</p>
  
  <p><strong>Edit:</strong> Future visitors, the answer to this is: instead of specifically watching for changes, when a user sends a change in, you send the change to open connections. </p>