Each HTTP request is a new request to the server. It is only possible to "simulate" sessions thanks to the server-side languages that support this, as is the case with PHP. But this is only a "current session" request for PHP, the apache server interprets it as a new request and has no "clue" that user was already "using" the services.
So two people using the site for two hours with the refresh of 1 second, for the server will be 14400 independent requests and there is nothing that can be done about it except the following solutions:
- Increase the request time (5s would be fine, but it depends on your need, in case a chat is not viable);
- Hire a hosting that has unlimited traffic. (Indispensable).
An interesting observation is that DoS attacks are quite similar to what they were doing, except that they see in a much larger number of requisitions in a much shorter period of time. In other words, your own site was "attacking" and the reason comes next.
Another problem is the use of the setInterval
function that sends a new request before the same one as the previous one ends (in the case of asynchronous requests). Create a recursive function using setTimeout
as already quoted in the comments by Guilherme Nascimento .
AtualizaDados = function(){
var endereco = 'https://fiddle.jshell.net/';
var params = {param1: 1, param2: 2};
var time = 3000;
var el = $("#mensChat div._5chat");
$.ajax({
url: endereco,
type: 'GET',
dataType: 'html',
data: params,
success: function(html){
el.html(html);
},
error: function(x, s){
console.log(s, x);
},
complete: function(){
setTimeout(AtualizaDados, time);
}
});
};
AtualizaDados();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><divid="mensChat">
<div class="_5chat"></div>
</div>
A good practice is also to reduce the amount of data to be trafficked, that is, remove the "junk" from your requests, start using JSON to traffic the data, instead of downloading the HTML content they would have for example of 300 to 500 bytes (each character is a , including whitespace
) would have only about 80 to 120 bytes (depending on message size), and then mount the HTML on the client side, for example:
Server:
<?php
/* Executa todas as ações necessárias */
header('Content-Type: application/json');
echo json_encode(Array(
'autor' => $autor,
'msg' => $msg,
'time' => $time,
'etc' => $etc
));
exit;
Client:
<script type="text/javascript">
AtualizaDados = function(){
var endereco = 'http://endereco/da/application/';
var params = {param1: 1, param2: 2};
var time = 3000;
var el = $("#mensChat div._5chat");
$.ajax({
url: endereco,
type: 'GET',
dataType: 'json',
data: params,
success: function(json){
el.append(
$('<div />').addClass('mensagem')
.append( $('<span />').addClass('autor').text( json.autor ) )
.append( $('<span />').addClass('msg').text( json.msg ) )
.append( $('<span />').addClass('hora').text( json.time ) );
);
},
error: function(x, s){
console.log(s, x);
},
complete: function(){
setTimeout(AtualizaDados, time);
}
});
};
AtualizaDados();