I am trying to get latest messages from the MySQL table using long-polling .
Here if i haven't got any new messages i am making script sleep()
for 10 seconds.
But in the worst case scenario if i don't receive any new messages for 10 minutes the script will say max_execution_time
exceeded.
How will i fix this . Can anyone say where i am going wrong.
PS - I don't want to use Websockets for now atleast.
pullmessage.php
$notyetgot = true;
$data_msg = mysqli_query($con,"SELECT * FROM messages WHERE message_id>".$latest." ");
$n = mysqli_num_rows($data_msg);
if($n > 0){$notyetgot=false;}
while($notyetgot){
sleep(10);
$data_msg = mysqli_query($con,"SELECT * FROM messages WHERE message_id>".$latest." ");
$n = mysqli_num_rows($data_msg);
if($n > 0){$notyetgot = false;}
}
...
(First, I hope I am understanding correctly the purpose of your code.)
PHP is designed to execute on-demand. A request comes in, it briefly does something, then goes away. To, instead, have it sitting there waiting for something is "wrong".
If, for example, you are building a feature similar to stackoverflow's "recent inbox messages", that is probably implemented by client-side Javascript code, which is "sitting there waiting" (waiting for the user to close the page).
Stack overflow is impressive with how many bits of information it puts on the page, and does it reasonably rapidly. A lot of sites perform poorly on less complex pages.
Back to the JS "solution". The JS code would periodically send an AJAX request to a PHP(?) page on some SO server. PHP would start, connect to the database, fetch the stuff requested, send it back to the client, and die. Then the JS would modify the DOM, and poof, the 'inbox' turns red and the number increments by 1.
It's a lot of work, and a lot of moving parts. But I think the initiation of the task starts on the client, not the server.
Note, also, that once PHP goes away, it has no way to reconnect to the client's web page. AJAX serves that purpose, but works in the "opposite direction".