I developed an AJAX based chat to meet my application
Here is what I do as far the chat is concerned
I keep the data of the chatroom (state of the chatroom in a database in XML format which is as follows)
<ChatRoom roomid="<roomid>">
<Users>
<User uid="<uid>" username="<username>" color="" heartbeat="">
<User uid="<uid>" username="<username>" color="" heartbeat="">
<User uid="<uid>" username="<username>" color="" heartbeat="">
</Users>
<Messages>
<Message id="" from="<username>">This is message text</Message>
<Message id="" from="<username>">This is message text</Message>
<Message id="" from="<username>">This is message text</Message>
</Messages>
</ChatRoom>
whenever a user clicks on a chatroom ... I basically send a get request to the server with the uid of the user ... the server will add the user information in the backend and also send me a list of users currently online (online is defined as any user who has a heartbeat within last 15 minutes) and also send the lastmessage ID of the chatroom
now I create an AJAX request and send the request every few seconds ... the request basically sends the lastMessageID known to the user ... if there is anyting new to the chatroom after that message ID (messageIDs are incremented everytime a new message is added to the chatroom ... and also I have wraparound in place to limit max number of messages == 100)
whenever new messages are delivered by the server ... user's browser view gets updated with new messages and also user updates the lastMessageID that it knows
I hope this clears the picture a bit
My questions are:
any comments on my approach and how am I doing things??
I would love to answer any questions and discuss details with you guys
Thanks a lot for your time!!
Keeping data in XML in a database seems overkill, use either a flat XML (or JSON!) file or just a database table, but probably not both. You can use long polling/Comet to work with callbacks/lighten the load on your server from the polling. Otherwise, looks sane.
See here, for example: http://en.wikipedia.org/wiki/Comet_%28programming%29