Chat system in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
varun8211
Forum Newbie
Posts: 1
Joined: Wed May 24, 2006 12:15 am

Chat system in PHP

Post by varun8211 »

hi,
I am trying to create a chat room in PHP.. I tried an open source PHP121 chat that uses AJAX. I could understand the working but, the thing that I disliked was the use of database to store messages between users.. I think, it would not be a good practice to access db for each message that is forwarded to the users chatting.. (it may create too many db connections and many transactions simultaneously when the more than 100 users chatting at same time.. I dont know, if my concept it clear .. plz elaborate, if I am wrong)..
Now, the next step I tried was to use XML to store and retrieve messages but this time, I was not able to find the DOM API.. (if anybody knows, plz help me).
My next step was to use simple text file to store messages but then I stumble upon the problem of multiple users accessing (read/write) 1 file at the same time..

I am still not sure, which is the best method to incorporate a chat system (1 to 1 chat only) in PHP..
Should I use Socket programming in PHP ?
I found this article recently posted here "http://www.devshed.com/c/a/PHP/An-Intro ... ts-in-PHP/".

Any help would be greatly appreciated..
thanks
User avatar
neogeek
Forum Newbie
Posts: 24
Joined: Sun May 14, 2006 4:24 am

Post by neogeek »

Check out http://betamasters.getdata.be/

Hope that helps.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Chat system in PHP

Post by timvw »

varun8211 wrote: I think, it would not be a good practice to access db for each message that is forwarded to the users chatting.. (it may create too many db connections and many transactions simultaneously when the more than 100 users chatting at same time..
Isn't that what databases have been built for?
varun8211 wrote: Now, the next step I tried was to use XML to store and retrieve messages but this time, I was not able to find the DOM API.. (if anybody knows, plz help me).
Have you already tried to do access (and modify) an XML file with a couple of concurrent users? I'm pretty sure you don't want to roll your own transaction mechanism either...
varun8211 wrote: My next step was to use simple text file to store messages but then I stumble upon the problem of multiple users accessing (read/write) 1 file at the same time..
It seems you've already experienced that transactions, or concurrent access can be a PITA. And yes, that's exactly what most DBMS have been built for..

varun8211 wrote: Should I use Socket programming in PHP ?
How would it solve your problem?



If i get it right, you currently have many users, that can send messages to other users (currently to only one other user).

Basically, you have a table users (user_id, username, password, bio, ...)
And you have a table for messages (sender_id, recipient_id, messagebody, sendtime)

Now it's pretty simple: When a users sends a message, simply INSERT INTO the messages tables...
When a users requests message: SELECT from messages and DELETE where message_id IN (SELECT from messages)
Post Reply