I want to add "live notifications" feature for my users: When they receive a new message(via the chat system) then display a notification badge on the chat icon in the navbar.
I was thinking to do: AJAX calls with setInterval:(As seen on another post here)
The AJAX call:
setInterval('checkUpdates', 2000);
function checkUpdates() {
$.get('/check_updates.php?timestamp=' . lastTime, function (results){
// do stuff here
}
);
The PHP code:
$timestamp = $_REQUEST['timestamp'];
$results = query('SELECT message FROM messages WHERE messageTime > "$timestamp"');
echo fetch($results);
I have a few questions before:
1) Since I want the update-checking to be constantly running, and the navbar is always present (always on the top of the page), is the navbar.php file the correct place to put that AJAX call?
2) How badly does that affect performance of the website, to have AJAX calls constantly running every 2 seconds?
3) Is there a better way? Do websites like Facebook have an efficient way to immediately popup the chat window whenever you get a new message, or it's thanks to their huge amount of servers?
Thanks