0

Hello guys,

I try to implement a chat application using PHP + MySql. The problem so far is that when I am chatting, I "lose" inputs when a small part of the page goes to be updated. On the other web browsers (Chrome, IE, Opera) I have some delay but at least the words are correct. I think the problem is with the

setTimeout("showMessages()",3000);

but I don't know how to improve the source code or if I have to use something else.
I try to make it as reliable as possible. I don't want to increase the refresh time because when a user "says" something, then the others must be able to see it almost simultaneously.
Any idea please??


Here is the source code:

function keyEvent(e)
        {
            if (window.event)
            {
                e = window.event;
            }
            if (e.keyCode==13)
            {
              send();
            }
        }


function getObject()
{
 if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) return new XMLHttpRequest();
    else {
    alert("Ajax is not supported, try a different browser.");
    return null;
    }
}

function showMessages()
{
    httpObject = getObject();
    httpObject.open("GET", "apath/show-messages.php?", false);// + Math.random(), false); 
    httpObject.send(null);
    document.getElementById("messages").innerHTML = httpObject.responseText;
    setTimeout("showMessages()",3000); //refresh the messages every 3 seconds
   
}

showMessages(); 

function send()
{
    httpObject = getObject();
    httpObject.open("GET", "apath/send.php?message=" + document.getElementById('message').value, false);
    httpObject.send(null);
    document.getElementById("message").value = "";
}
2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by johny20
0

Use a cookie to store the id of the last message retrieved. When you go to retrieve messages, grab all the messages that are greater than the id of the message in the cookie and replace this id with the highest id retrieved. By doing it this way you are only ever retrieving new messages. (this is assuming that your table in your database has an unique id column for each message.)

0

Thanks for the reply.
But this is not what I want because I want all the conversation to be available. Also, I checked it with empty table (so, no data to retrieve every 3 seconds) and I had the same problem. If I try to type the numbers between 1 and 9 there are always 1 or 2 numbers missing from my text field! It's completely annoying!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.