title: Websockets PHP / AJAX / Javascript refresh client
body: <p>I'm using websockets with PHP and some Javascript. I display some information from my database to my client. My goal is to refresh the client when an information has insert into my database. To insert some data in my database, I send a post request from my PC.</p>
  
  <p>At the moment, I can refresh the client to call a function every 10 seconds. But I would like to refresh only if, on that page, a POST request is send.</p>
  
  <p>Here is something that can help you to understand:</p>
  
  <p><img src="https://i.stack.imgur.com/PGUym.png" alt="enter image description here"></p>
  
  <ol>
  <li>The client's connecting to the web page.</li>
  <li>A SQL select is execute on my database</li>
  <li>The result is received and displayed on the web page. </li>
  <li>The client is connected (via websockets) and see the information from the database.</li>
  <li>Me. If I insert an information on the database, I send a POST request (not from a HTML form, no submit, just a POST request, like if I used Postman to test a webservice). When the request is send, refresh the client by calling a Javascript function...</li>
  </ol>
  
  <p>Here is what I've done:</p>
  
  <ul>
  <li><p><strong>index.php</strong> (Page where the information are displayed)</p>
  
  <pre><code>$(document).ready(function(){
  
      //create a new WebSocket object.
      var wsUri = "ws://localhost:9000/server.php";
      websocket = new WebSocket(wsUri);
  
      websocket.onopen = function(ev) { // connection is open
          $('#message_box').append("&lt;div class=\"system_msg\"&gt;Connected!&lt;/div&gt;"); //notify user
      }
  
      // Call this function every 10 sec to refresh the client
      window.setInterval(function(){
          displayInfo(1);
      }, 10000);
  
      websocket.onmessage = function(ev) {
          var msg = JSON.parse(ev.data); //PHP sends Json data
          $("#infos").html(msg.message);
      };
  
      websocket.onerror   = function(ev){$('#message_box').append("&lt;div class=\"system_error\"&gt;Error Occurred - "+ev.data+"&lt;/div&gt;");};
      websocket.onclose   = function(ev){$('#message_box').append("&lt;div class=\"system_msg\"&gt;Connection Closed&lt;/div&gt;");};
  });
  
  function displayInfo(r) {
  
      $.post("infos.php",
          { refresh : r},
          function(data){
  
              var msg = {
                  message: data
              };
  
              //convert and send data to server
              websocket.send(JSON.stringify(msg));
          }
      );
  }
  </code></pre></li>
  <li><p><strong>infos.php</strong> : Where I execute the SQL select request (works fine)</p></li>
  <li><p><strong>server.php</strong> : Where the code for the server side is. (works fine)</p></li>
  </ul>
  
  <p>Well. I really don't know how can I achieve this. How to inform and call the function displayInfo(r); if a POST request is send to this page.</p>
  
  <p>Any help is very appriciated. Thank you in advance.</p>
  
  <p>Regards,
  Lapinou.</p>