title: Chat using PHP, websockets and MySQL
body: <p>I'm trying to write a chat using this <a href="https://github.com/ghedipunk/PHP-Websockets" rel="nofollow noreferrer">https://github.com/ghedipunk/PHP-Websockets</a> PHP class. Now it looks like, on beginning JS on client side connect to server, then it sends a request once a 3 seconds, and server on every request check messages in database, and send messages to client. I think it's not good, that client sends request once a 3 sec, and I wonder how to do, that server send message to client, only when there is a new message in database? 
  This is my extended process function:</p>
  
  <pre><code>protected function process($user, $message) {
      $databaseHandler = mysqli_connect('blahblah');  
      $decoded = json_decode($message, true);
      $chat_id = $decoded['chatid'];
      $limit = $decoded['limit'];
      $user_id = filterString($decoded['userid'], $databaseHandler);
      $SQLQuery = "SELECT chat_messages.message, users.name FROM chat_messages JOIN users ON chat_messages.user_id=users.user_id WHERE chat_messages.chat_id = '$chat_id' ORDER BY chat_messages.message_id DESC LIMIT $limit,5;";
      $SQLResult = mysqli_query($databaseHandler, $SQLQuery);
      $i = 0;
      $arr = array();
      while ($row = mysqli_fetch_assoc($SQLResult)) {
          $i++;
          $arr[$i] = $row['name'] . ': ' . $row['message'];
      }
  
  
      $this-&gt;send($user, json_encode($arr));
  
  }
  </code></pre>
  
  <p>This is JS function:</p>
  
  <pre><code>function refreshchat() {
  
  try {
      socket.send('{"token":"' + token + '","userid":'+ userid +'"chatid":' + chatid + ',"limit":' + limit + '}');
  
  } catch (ex) {
      console.log(ex);
  }
  setTimeout(refreshchat, 3000);
  </code></pre>
  
  <p>}</p>
  
  <p>I skipped authorization stuff in php for clearance.</p>
  
  <p>Thank you in advance!</p>