title: Pushing data from one client to another using Php
body: <p>I am making my college project on Air Quality Monitoring System, in that data (say some integer value) has to be taken from sensing unit to a webpage. </p>
  
  <p><strong>What I want</strong></p>
  
  <p>Is that the script called upon by this url <code>http://localhost/AQProject/recordupdate.php?val=2</code> updates a web page displaying the content. Now I know I can save that data in database and run ajax based query every two seconds to check for update, but I want that update to be pushed by server.  </p>
  
  <p><strong>What have I done:</strong></p>
  
  <p>I have tried <code>Server sent events</code>. Here's what i tried </p>
  
  <pre><code>&lt;?php
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache');
  
  if($_SERVER["REQUEST_METHOD"]=="GET")
  {
      if(empty($_GET["val"])) die ("Empty Value from source");
      else
      {
          $v = $_GET['val'];
          echo "data: The Pollution stub value is {$v}".PHP_EOL;
          ob_flush();
          flush();
      }
  }?&gt;
  </code></pre>
  
  <p>and html has script </p>
  
  <pre><code>    &lt;script&gt;
  if(typeof(EventSource) !== "undefined") {
      var source = new EventSource("recordupdate.php");
      source.onmessage = function(event) {
          document.getElementById("result").innerHTML = event.data + 
          "&lt;br&gt;";
      };
  } else {
      document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
  }
  &lt;/script&gt;
  </code></pre>
  
  <p>Now, I have figured out this much that (correct me If i am wrong) it won't work because when another client (sensing unit) calls for <code>recordupdate.php</code> its a different instance of that script than that called by webpage client. </p>
  
  <p>Is there any possible way of doing this using <code>server sent events</code>? Or I absolutely need to dig into websockets, node.js etc. Thanks in advance</p>