I’m working on a simple webchat application for my website. However, for me, it feels wrong using interval and requesting the chat with ajax every second?
What I’m doing today is this,
<script>
$(function() {
$('#chat_form').submit(function() {
$.post('chat/post', $('#chat_form').serialize(), function(data){
var message = $("#message").val();
$("#message").val('');
$("#chat_main").append('<li><b>Bruker: <?php echo " ".$this->session->userdata("chat_username") ?></b>: ' + message + '<br/></li>');
});
return false;
});
function loadchat()
{
$('#chat_main').load('chat/load');
}
setInterval(loadchat, 500);
loadchat();
});
</script>
Is there any other better way?
This is really cool article regarding chat application and best way to implement comet using jQuery.
Its works for me.
There is few hack needed in order to make it perfect because its work in timestamp so when you change your machine date its fail to perform but on server its work fine.
Replace text file implementation using xml that is good practice.