title: Sending data from server to client?
body: <p>I have a php server file and an HTML client file, the HTML file send ajax requests to the server to retrieve data every <code>500 ms</code>, this although works as expected it's causing high usage of memory and CPU on the client's device.</p>
  <p>PHP</p>
  <pre><code>if(isset($_POST['id']) &amp;&amp; $_POST['id'] != '' )
  {
      $id     = $_POST['id'];
      $select = $con-&gt;prepare(&quot;SELECT * FROM data WHERE id=?&quot;);
      $select-&gt;bind_param('s', $id);
      $select-&gt;execute();
      $result = $select-&gt;get_result();
      while($row = $result-&gt;fetch_assoc())
      {
          echo $row['column 1'] . &quot;\t&quot; . $row['column 2'] . &quot;\n&quot;;
      }
  }
  </code></pre>
  <p>AJAX</p>
  <pre><code>function send(){
      var formdata = new FormData(),
          id       = document.getElementById('id').value;
      formdata.append('id', id);
      var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
      xhr.open('post', 'server.php', true);
      xhr.send(formdata);
      xhr.onreadystatechange = function(){
          if(xhr.readyState == 4 &amp;&amp; xhr.status == 200){
              console.log(xhr.responseText);
          }
      }
  }
  setInterval(function(){send()}, 500); 
  </code></pre>
  <p>I would like to find an alternative solution to ajax, instead of sending numerous requests to the server and retrieving same data most of the time, it would be much more efficient if the server can interact with the client on data change or update.</p>
  <p>I can't use PHP <code>Socket</code> or <code>HttpRequest</code> methods as they are not installed on my hosting server and I'm not sure if the later works. The only way I can think of is using <code>SESSIONS</code>.</p>
  <p>According to <a href="https://stackoverflow.com/questions/675913/looping-through-all-a-servers-sessions-in-php">this</a> PHP server store all users sessions on the same directory on the server, therefore it may be possible to change sessions variables for a particular user directly on the file. The problem however is the data in those files are serialized and I'm not sure how to de-serialize the data and re-serialize them and then save the new data!</p>
  <p>Even if I was able to find a way to store updates on the session file, I still need to use setInterval to listen to the session's variable change every <code>500ms</code> although it's not ideal but it would be much better than using <code>XMLHttpRequest</code> in terms of memory and CPU usage.</p>
  <p>So what's the best way to do this? any help would be much appreciated.</p>
  <hr />
  <p><strong>UPDATE:</strong></p>
  <p>I realized that <code>SESSION</code> wont work because it can be read only by the server not the client, therefore i have to send ajax request to the server to get the variables which i was trying to avoid.</p>
  <p>I tried long polling but i had many problems with it, <code>flush</code> and <code>ob_flush()</code> doesn't work on my server and i can't change the <code>ini</code> settings. When trying the infinite loop i can't get it to break on data change:</p>
  <pre><code>if(isset($_GET['size']) &amp;&amp; $_GET['size'] != '')
  {
      $size = (int)$_GET['size'];
      $txt = &quot;logs/logs.txt&quot;;
      $newsize = (int)filesize($txt);    
      while(true) {
          if($newsize !== $size) {
              $data = array( &quot;size&quot; =&gt; filesize($txt), &quot;content&quot; =&gt; file_get_contents($txt));
              echo json_encode($data);
              break;
          }
          else{
              $newsize = (int)filesize($txt);
              usleep(400000);
          }
      }
      
  }
  </code></pre>
  <p>it keeps going on and on, even if the <code>logs.txt</code> size increase it won't break! how can I make it break and echo data on size increase?</p>
  <p><strong>UPDATE 2:</strong></p>
  <p>It turned out the php cache the filesize when calling <code>filesize()</code> method therefore the above loop will run indefinitely, the solution for that is to use <code>clearstatcache()</code> method which will clear the stored cache of the file size allowing the loop to break on filesize changes.</p>