I'm developing a webchat website using AJAX, PHP and MySQL.
But I encounter a problem, there is a function which checks whether the person you are chatting with is still on chat (online) or he/she has dropped the chat. The function looks like this:
//some variables here
var somedata="....";
var userleftchatmsg="he/she left you,you are alone now.";
function checkifuseronline(){
$.ajax({
type: "GET",
url: "check.php",
dataType: "json",
data:somedata
}).done(function(r) {
if (r.o1==2) {
$("#chattable").prepend(userleftchatmsg);
} else {
checkifuseronline();
}
});
}
The thing is that "he/she left.." message appears twice sometimes, especially when a user chats with multiple partners at the same time. Isn't that supposed to be impossible or am I wrong?
You might not like this answer, but check out XMPP. It's designed to solve this problem, and all the others you haven't run into yet. http://xmpp.org/about-xmpp/
Alternatively, a more direct route would be to use a NodeJS server to do this: http://net.tutsplus.com/tutorials/javascript-ajax/real-time-chat-with-nodejs-socket-io-and-expressjs/
The sucky answer is PHP can't do what you're trying to do. It's a great language I use all the time, but you can't get there from here.
Good luck!