Hi all, i am making one site for social networking with some gr8 features. now i want to add a chat room in it. but i think using a file system or table for chat isnt a good idea as both have lot of cons. i want a chat systme in which people who has joined a room can send recieve messages on a group i dont have any intention for PM in room. just a simple chat room. i can make it in ajax using files or table but that can heavily effect the database considering a good amoun of traffic. i mean in start it iwll run smootly but with increase of traffci its gonna make problems.
one blue idea i have is to use session variable that will be stored in every session user and will be deleted once user logs out etc.
any help will be appreciated

Recommended Answers

All 5 Replies

session is stored per user wise.
So u can not store chat in session as the other user chatting with user can not use each other's session.

Effectively you need to store the data somewhere.
You can store them on a database, (easiest solution but traffic problems)
xml file (still easy and fast loading but prone to hacking and directory traversal)
or in cookies on each users computer (a minor hacking threat but effecient, however when user logs off so will the entire chat by them)

pretty much a database is the most effecient. set up a cron-job to delete records half an hour old or whatever.

If your not going to have a moderator in your chat room you might like to block specific words or sentenes from input.

Sessions by default PHP settings will store to files. As already mentioned, it is per user only.

If you don't have root access:

You can however store the chat messages in memory only. Probably the simplest way to do this is to store your database table in memory.

Mysql has a table storage engine called "Memory" which stores in memory. I believe SQLite also has a memory based storage.

If you have root access:

You can also store messages in a file that is actually in memory. You can search google for how to do this ('memory mapped files, tmpfs etc.')

Note that the only reason you are storing the messages (even in memory) is to distribute it to the chat clients. Since PHP is run in a separate process, you don't have access to memory (PHP variables) in another process (another chat client request). Thus you have to save the messages outside the process, and show it to the client by retrieving it from storage.

You can however bypass storing by either using a single process, or inter process communitcation (IPC). You can write a socket server in PHP, and have the chat clients connect directly to that (with a TCP connection, or UDP etc.). PHP also has extensions for IPC, or even a caching solution such as APC or Memcached could be used for IPC. This is quite extensive for a "simple" chat solution however and if you're going to go that far there are better solutions such as just deploying an XMPP server (open source IM chat server).

This reminds me:
I wrote a rudementary PHP cache that does not require root access a while ago. It does require sockets enabled however.


It could be used for the IPC if you needed. It is actually simpler then it sounds. You just need to store messages to it, and retrieve messages:


 $Cache = new Socket_Cache_Client('', 9803); 

$messages = $Cache->get('messages');

$messages[] = 'This is a new chat message just added at '.date(DATE_RFC822);
$Cache->set('messages', $messages); 


You just have to make sure to start the socket server as described on the project page.

Thanks for all your support and help i will now chekc the new methods provided in last two messages.
thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.