If dealing with an application developed for the web, there is no way to get away. The only ways to create updates for the client are:
Create business rules on the client's own computer (Solution in case the client will perform an activity on his own machine, such as being alerted after completing a form correctly or parsing a file via js).
Hitting the server via ajax, requesting and handling the incoming data. (I believe this should be your approach)
Okay, but what about "server grief"?
The purpose of ajax is to minimize the amount of data traffic, in order to make small requests to the server and take advantage of the data already provided. So the solution would be to create an endpoint to fetch only the required data. Example:
Client computer performs the request:
$.ajax({
type: "POST",
url: "/checkUpdates",
data: {
userId: 1,
lastMessageId: 1
},
dataType: "json",
success: function(response) {
verifyAjaxReponse(response);
}
});
You collect the data sent by the user, and compare them with the one received:
$data = json_decode($params); //dados vindos pelo ajax
$result = $db->exec('select lastMessageId where userId = "' . $data['userId'] . '"');
if ($data['lastMessageId'] == $result['lastMessageId']) { //Supondo que a request não passe por aqui, e exista um novo resultado
$result == false;
}
$framework->disableLayout();
$framework->view = json_encode($result); // $result = ['message' => 'Você recebeu uma nova mensagem. Verifique sua caixa de entrada', 'id'];
After that, the data will be minified and returned to the client machine, where your javascript can check if the response has any updates and generate the alert.
success: function(response) {
verifyAjaxReponse(response);
}