I developed an AJAX based chat to meet my application
Here is what I do as far the chat is concerned
- There are various chatroom and a user can click on any chatroom to start chatting
- A user logs in and can be part of only one chatroom at a time
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:
- from a design perspective ... how is the design
- where I can make improvements
- for chat to be responsive I have found that I need to check for new messages every 2 seconds ... which may generate unnecessary request load on the server
- this is a polling mechanism ... is there a notification mechanism ... like a client setting a callback ... and server replying to the callback whenever there is something new
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!!