Search code examples
phpmysqlajaxlong-polling

Notifications of new messages. Long polling


Help me please to realise notifications of new messages for users.

Now i have this client code:

function getmess(){
$.ajax({
    url:"notif.php",
    data:{"id":id},
    type:"GET",
    success:function(result){
        $("#count").html(result);
        setTimeout('getmess',10000);
    }
  });
}

and this server code:

    $mysqli = new mysqli('localhost', 'root', '', 'test');
if (mysqli_connect_errno()) { 
    printf("error: %s\n", mysqli_connect_error()); 
    exit; 
}  

session_start();

$MY_ID = $_SESSION['id'];

while (true) {
$result = $mysqli->query("SELECT COUNT(*) FROM messages WHERE user_get='$MY_ID'");
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_array($result)) {
echo $row[0]."";
 }
flush();
exit;
}
sleep(5);
}

I have the problem that this script is not updating in real time when new message was added to database. But if I press button with onclick="getmess();" it works.


Solution

  • First, you check your database every 5 seconds, so you can't achieve real time - you have at least 5 seconds delay.

    And second, there is no way you can achieve real-time by polling.

    The way to deliver notifications nearly real time is to send the message by the same code that inserts into the database, e.g. you should not query the database for new records, but when there is a new record to send the data to the client. Even with a long-polling as a transport protocol.

    How to achieve this? Unfortunately PHP is not a good choice. You need a non-blocking server to hold the connection, you need to know which connection waits for what data and you need a way from PHP (your backend) to notify this connection.

    You can use the tornado-web server, node.js or nginx to handle the connections. You assign an identifier to each connection (probably you already have one - the userid), and when there is a new record added - the PHP script performs HTTP request to the notification server (tornado, node.js, nginx) saying what data to which user does this.

    For nginx, take a look at nginx push stream