views:

287

answers:

3

im going to use ajax/comet to create a chat. and i want to store the chat conversation.

every keypress will trigger an event that sends to backend.php that will store the letter. i wonder if i should store it directly in the database or in the textfile.

you dont have to consider the details for exactly how this chat's architecture will be. just that every letter pressed will be stored immediately.

i thought that if storing in mysql (im using php on a comet server) then there will be a lot of queries, just for one letter each. and if a lot of users are chatting, then it will be a heavy load with UPDATE and SELECT queries.

will storing the conversation in text files be easier for the server? just open it, append the letter and close it with php.

what do you think?

+1  A: 

Question: live chat: saving directly in database or text file?

Answer: If all you have is random chat text, a text file is probably perfect.

Justification: What possible SQL query would you ever use on this data?

At best you might use something like grep to scan the file.

Why bother with a database unless you have really complex data with a lot of interesting attributes?

Example:

select username,count(*) as most_active_users from chatroomxxx group by username limit 0,10;

Python:

user_fq = collections.defaultdict( int )
for path, dirs, files in os.walk( 'path/to/chat/logs' ):
    for fn in files:
        with open( os.path.join( path, fn ), "r" ) as source:
            for line in source:
                user, timestamp, text = line.split('\t')
                user_fq[user] += 1
fq_user = collections.defaultdict( list )
for user, fq in user_fq.items():
    fq_user[fq].append( user )
top = []
for fq in sorted( fq_user, reverse=True ):
    top.extend( fq_user[fq] )
    if len(top) > 10: break

The point is not that Python is as terse as SQL. The point is that the query is relatively simple data gathering from text files.

And simple data gathering from text files is FAST. Far, far faster than a database. And with essentially zero overhead.

Just write the lines to files. Scan the files with simple scripts. No overhead.

S.Lott
it could be thread contents too.
weng
ok then, i think i will save the contents in data files so other will see as a user type, then save it all to the mysql when the user clicks submit. does this sound good?
weng
I don't get why MySQL is involved in anything except -- perhaps -- session management.
S.Lott
i have to save the thread content in mysql ?
weng
Why? This is the fundamental thing I do not understand. Why put the thread content in a database? The database does not appear to be helping you. There does not appear to be any use case for a database. So why bother?
S.Lott
+1  A: 

There will be as many file operations as sql operations. Files may be quicker for some purposes, but databases will scale much better.

I'm surprised you're firing every keypress - do you know how fast some users type??? With Ajax being asynchronous (by default), you will quickly find that your requests are stacking up and this will delay the message being delivered to the chat partner as well as place huge load on your server.

You could downgrade to sending after every word, or even on a return keypress.

adam
ok i will trigger event after 1 sec of inactivity then. so if he types 4 letters within a second then they will be sent 1 second afterwards he has stopped typing.
weng
I agree with adam. Even Google Wave, I'm guessing, doesn't send every single keypress--more likely it sends the queued keypresses every 500ms, for example.
Jordan
+2  A: 

I would not send every letter over the network, this will needlessly generate a large amount of network traffic and database/file writes. Instead send the message when the user has committed to it by pressing a Send button or the Enter key.

Storing in a database makes much more sense than a file, as you will need to be able to index the data for quick retrieval. Consider that you will need to retrieve data in groups, not as it was sequentially added to the file.

RedFilter
but i want other users to see every letter he types. like google wave=)
weng
I think it is annoying watching other people typing and correcting their mistakes, or pausing in the middle of a word while they eat their sandwich. I only want my attention to be grabbed when there is something I can read in one chunk, i.e., a whole thought. But if that is what you want, more power to you!
RedFilter
well...i like google wave, so live=)
weng
if i instead have it like this: the user types, and it will be saved in a text file so other could see the message while he is typing, not until he clicks SAVE its saved in the database and the text file is deleted. what do you think?
weng
Why not set a timer for when the user types something, resetting it when he keeps typing. You could also and/or send when 'n' letters have been entered. That way, you can send a sequence of letters in one go (which would be acceptable for someone watching it)...or when the user pauses typing.
cmroanirgo