Note that there are some explanatory texts on larger screens.

plurals
  1. POServer-Sent Events from PHP polling mySQL
    primarykey
    data
    text
    <p>A back-office periodically needs to sent data to clients connected to a web application. Here is the script I have setup:</p> <pre><code>&lt;?php header("Content-Type: text/event-stream\n\n"); header('Cache-Control: no-cache'); flush(); ob_flush(); require_once('connect.php'); //connect to the database $mysql = mysql_connect($hostname, $username, $password) or die ('cannot reach database'); $db = mysql_select_db($database) or die ("this is not a valid database"); mysql_query("SET NAMES 'utf8'", $mysql); $query = mysql_query("SELECT * FROM `clientEvents` ORDER BY id DESC LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); $index = $row["id"]; echo $index; loop($index); function loop($index){ while (1) { ob_implicit_flush(true); $buffer = str_repeat(" ", 4096); echo $buffer."\n"; // Every second, look for new question. $query = mysql_query("SELECT * FROM `clientEvents` WHERE id&gt;$index ORDER BY id DESC LIMIT 1") or die(mysql_error()); $row = mysql_fetch_array($query); if(mysql_num_rows($query)&gt;0) { if($row["type"]=="pushCustomQuestion"){ pushCustomQuestion($row["data"],$row["id"]); break; } else if($row["type"]=="pushGameTimeEvent"){ pushGameTimeEvent($row["data"],$row["id"]); break; } } sleep(3); } function pushGameTimeEvent($id,$index) { //echo "id for game event:".$id; $result = mysql_fetch_array(mysql_query("SELECT * FROM game_events WHERE id = $id LIMIT 1")); //$num_results = mysql_num_rows($result); echo "event: new_game_event\n"; echo 'data: { "id": "' . $result['id'] . '",' .'"match_id": "' . $result['match_id'] . '",' .'"minute": "' . $result['minute'] . '",' .'"event_id": "' . $result['event_id'] . '",' .'"event_name": "' . $result['event_description'] . '",' .'"player_name": "' . $result['player1_name']. '",' .'"player2_name": "' . $result['player2_name'] . '",' .'"which_half": "' . $result['which_half'] . '",' .'"team_logo": "' . $result['team_logo'] . '"}'; echo "\n\n"; ob_flush(); flush(); sleep(10); loop($index); } </code></pre> <p>Everything seems to work OK up until seven clients load the web application where everything hogs. Is there something wrong with the script as it is?</p> <p>It seems to me that the way I'm doing it is counterintuitive since I'm polling the database in order to send a new event. Is there some other better way to trigger the event from the back- office application?</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload