Monday, October 30, 2006

Puakma: Under the hood:

3,001 Ajax web chat applications have been written. It's almost a right of passage for developers learning the capabilities of the Ajax paradigm. I have been putting off writing one because there were already 3,000 in existence, but last week I bit the bullet and scratched my itch. Sometimes it's good to do stuff just because you can.

The problem I had to solve is: We are about to start a weekly Tornado meeting. I want everyone to be able to find the chat session easily and have it start at a designated time (and finish so noone can rejoin an old one later!). Further, everyone shouldn't need a fancy schmancy chat client and an account somewhere (eg, skype, msn, icq, ...).

So how does it work?

I have two tables, one called CHATEVENT which holds the meeting information such as start date, time, description etc. The other table is called CHATDATA and this holds the text people type, only row per chat message tagged to a CHATEVENT. At this stage I am not concerned about managing invitees but that could be easily added later. YAGNI

On the client side, each time you type some text in the input field, it is sent to hte server via an ajax request and poked into the CHATDATA field. There is also a client side thread that polls the server for new messages, reading the CHATDATA table to find any new messages since the last poll. Any new messages are returned and dynamically added to the div which holds the chat messages, creating the illusion of messages being pushed to the client.

The trick I found was to ensure everyone only uses the server time for timestamping messages and have the server return the time of the last message. If you don't you'll get things doubled up - or missed.

On performance, because the ajax chat mechanism works on a polling basis you need to tweak the poll interval so that each client will poll regularly enough so the user experience is nice and infrequently enough to reduce the server load.

Hopefully we can get this integrated into podmatcher soon and start using it as the chat medium, that way if you miss a meeting you can come back later and read the pod meeting's transcript.

If you'd like a copy of this Tornado app, email me :-)