I need help in doing an online chat. I already created a simple online chat where one user can communicate to another user and their conversation is saved as a json file. The json file is what I am reloading after every 2secs using setInterval(). The message the users are sending are appended on the json file by posting it using ajax and through php function fopen and fwrite(append).
My problem is that the sending/posting process on my ajax is too slow. It takes time before the user see his/her message was send on the chat screen(div). I tried appending the message on the screen to look as if it is already sent but the problem is when the setInterval reloads the chat screen and the new appended message is not yet saved on the json file, the newly sent message will not be included on that reload.
Questions:
Few Points to consider:
-> Search about Long Poll (COMET)
-> Why do setInterval refreshes the screen, it should just append the new messages from the server
-> If you are using HTML5 based browsers, check Websockets and Server Side Events.
-> Use DB operations on server side, instead of File operations. It will also improve maintainability, consider a case in which 1000 different users are chatting with each other, it will be difficult to maintain those files.
-> Server should send the delta messages which are new, it should not just send the whole conversation again to client to parse and reload.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With