Project: An application that requires a 1-1 private chat.
Current system: The current login system in the application uses a cookie system for the users to remain logged in.
I understand how to implement the chat system (I just read about Comet, and might implement it later, but for now, I plan to use basic AJAX using a timer); the chat will be stored in a text/xml file during the chat, and it will be archived into the database once it's finished. The filename will be stored in a database when the chat starts, and once retrieved, will be stored as a cookie (the database will be untouched, except for when the chat starts and ends).
Problem: A user cannot start conversations with another user unless they are both online. How do I design the system such that the chat can only start if both users are online?
I don't really need specific code, just the logic of making it work.
When a user is logged in your site and stays you send a ajax ping on every 5sec to server, and the server will update the time of the ping. And so you can list your online people looking on this time, and if the ping time is greater than 5 sec that means your user is logged out or closed their browser.
Also you can do the away status with the same mechanism using browser focus events.
Assuming you're logging user activity:
Only allow users to receive a chat if the receiver has showed activity in the past 30 seconds. If not, show an error message that the user isn't online and/or ignore the sent message.
With a stateless system, the only way you can decide who is and is not on line is by using a timeout. You mentioned using AJAX and a timer, so if the last user activity is older then the last AJAX refresh (plus the refresh time) then they are not on line. You need to make sure that the chat initiation page starts the activity updates.