Php – Is database based chat room bad idea

chatMySQLPHP

Most of simple "group chat" systems are based on local file storage logs (e.g. log.txt ).

I'm developing a simple group chat (under 20 users simultaneity ) with jQuery/AJAX.

So basically, client-side code loads log from server every second. It needs to be fast.

In database system (PHP/MySQL) that would mean MySQL query every second. Is that overload for server (shared hosting)?

Best Answer

I've done this before, in the days before AJAX frameworks were widely used. Using a database was fine. The trick is to make sure that each client knows the ID of the latest message that it's seen. Each time the client asks for new messages, it should submit this ID. Then when you query the database, retrieve only those messages whose ID exceeds the one provided by the client. This will be a very fast query, that should only return a small handful of rows at the most.