I’m making simple php chat, and i need to refresh messages in one div.I tried with jquery load function but it’s making computer slow.
Is there simple ajax refresh, something like facebook have in their chat ?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In case of a chat the best solution might be not to replace the whole DIV but to simply append new messages to the end. A more durable approach is then to keep a stack of say 500 messages and do a First In First Out operation with javascript.
The client then simply polls the backend for new messages only, ignoring the rest. This should make your back-end AND front-end fast.
Update:
For example, the HTML might look like this:
Then you would use some kind of polling technique, for example Prototype.js has a periodical updater available out of the box – http://prototypejs.org/api/ajax/periodicalupdater – which would send a request to your backend alongside a client id and a timestamp.
The backend then in turn needs to lookup if anything has changed since the last time the selected client did a request and send any new data back to the client.
The client side could then use Prototype.js’ insert (eg. http://bobobobo.wordpress.com/2008/05/22/the-stupidly-brief/) to simply add new messages to the end of the stack.
Apparently you could also count the number of LI elements in the UL and in case it is greater than X remove the oldest ones.