I have a website which is a messaging application.
Currently, it refreshes every 500ms by reading the outputs of the refresh.php file.
I would want to know if it is possible to call the refresh function everytime the 'messages' table updates.
Most of the answers include setTimeout or setInterval, but I want to refresh it only when the table updates, because last time my website got suspended for overloading the host servers.
chatbox (index.php):
<div id="chatbox">
<?php
if ($result = $mysqli->query("SELECT * FROM messages")) {
while ($row = $result->fetch_assoc()) {
echo formatMessageHTML($row["text"], $row["authorid"], $row["timestamp"], $row["hidden"], $mysqli);
}
$result->free();
}
?>
</div>
refresh.php
<?php
include_once "dbconnect.php";
$mysqli = new mysqli("localhost", $username, $password, $database);
if ($result = $mysqli->query("SELECT * FROM messages LIMIT 50")) {
while ($row = $result->fetch_assoc()) {
echo formatMessageHTML($row["text"], $row["edited"], $row["authorid"], $row["timestamp"], $row["hidden"], $mysqli);
}
}
$result->free();
function formatMessageHTML($text, $edited, $authorid, $timestamp, $hidden, $mysqli)
{
$html_message = "";
if ($hidden == 1) return;
if (!isset($text) || !isset($authorid) || !isset($timestamp)) return "<div class='msgln'><span class='chat-time'>" . date("g:i A") . "</span> <a class='taglink' href='javascript:insert_mention(\"Unknown\");'><b class='user-name'>Unknown</b></a> Unknown<br></div>";
if ($result = $mysqli->query("SELECT username, namecolor_fg, namecolor_bg FROM users WHERE id=" . $authorid)) {
while ($row = $result->fetch_assoc()) {
$tms = explode(":", $timestamp);
unset($tms[2]);
if($edited == 0) {
$html_message = "<div class='msgln'><span class='chat-time'>" . join(":", $tms) . "</span> <a class='taglink' href='javascript:insert_mention(\"" . $row["username"] . "\");'><b class='user-name' style='color: " . $row["namecolor_fg"] . "; background-color: " . $row["namecolor_bg"] . "'>" . $row["username"] . "</b></a> " . $text . "<br></div>";
} else {
$html_message = "<div class='msgln'><span class='chat-time'>" . join(":", $tms) . "</span> <a class='taglink' href='javascript:insert_mention(\"" . $row["username"] . "\");'><b class='user-name' style='color: " . $row["namecolor_fg"] . "; background-color: " . $row["namecolor_bg"] . "'>" . $row["username"] . "</b></a> " . $text . " <span class='edited'>(edited)</span><br></div>";
}
}
}
return $html_message;
}
?>
Javascript (index.php):
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script>
function loadLog() {
console.log("[DEBUG] Refreshed chat");
var oldscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height before the request
$.ajax({
url: "refresh.php",
cache: false,
success: function(html) {
$("#chatbox").html(html); //Insert chat log into the #chatbox div
//Auto-scroll
var newscrollHeight = $("#chatbox")[0].scrollHeight - 20; //Scroll height after the request
if (newscrollHeight > oldscrollHeight) {
$("#chatbox").animate({
scrollTop: newscrollHeight
}, 'normal'); //Autoscroll to bottom of div
}
}
});
}
setInterval(loadLog, 500);
</script>