Js:
<script>
function cometConnect(){
$.ajax({
cache:false,
type:"post",
data:'ts='+1,
url: 'Controller/chatting',
async: true,
success: function (arr1) {
$(".page-header").append(arr1);
},
complete:function(){
cometConnect(true);
nerr=false;
},
dataType: "text"
});
}
cometConnect();
</script>
Php:
public function chatting()
{
while(true)
{
if(memcache_get(new_message))
return new_message;
sleep(0.5);
}
}
Is this a better solution than setting setInterval, which connects to a PHP method that returns a message if there is any 1 second (1 sec. Increments +0.25 every 5 seconds, say)?
If I used the first solution, maybe I could use sleep (0.5), this would give me messages instantly because the php loop is cheap, right?
So which solution is better (more importantly, which requires less resources?). Because there will be hundreds of such chats.
Plus, can the first solution cause problems? Say I would reload the page, or I would stop execution every 30 seconds, so I would not get 502 Bad Gateway.
EDIT: , , , , ? - ?
, , , , .