Page 1 of 3
php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 8:49 am
by morris520
Hi
My task is to develop a instant messenger like facebook

using php and ajax.
The idea is to save the conversation in database and let the client to load the conversation in a certain time, e.g. every 2-5 seconds. while they can do other things at the same time.
so for the table conversation, it holds
id
timestamp
user1
user2 (only two because is one-one chat)
message
Right, the problem is, when many people are chating, the database is accessed by heavy load and looks like it takes long to load a message. (seems to loose the word 'immediate message').
So I am not sure my design is correct, and if there's any thing wrong to cause this problem? I've read some other threads about this topic but I think I didn't get the answer I want so please feel free to give your idea.
Your help is appreciate and thank you!
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 8:53 am
by arjan.top
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 9:22 am
by jackpf
How are you retrieving the messages?
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 11:22 am
by morris520
Hi arjan.top
The comet programming is implemented by Ajax or IFrame etc.
Thats what I am trying to do.
I will do further reading following that topic thanks.
By the way, any good example of implementation?
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 11:24 am
by morris520
Hi jackpf
My original thought is to retrive by using queries. But as I said, it looks to increase server loads....
Maybe I am wrong.
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 11:48 am
by arjan.top
with comet:
1. send ajax request (new message)
2. script that is handling requests sends new message to comet server (save to db for chat history etc.)
3. all users on some channel get the message
edit: look at meteor (
http://meteorserver.org/)
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 12:03 pm
by morris520
to arjan.top
Right, after reading some resources I got some feels that using comet is easy to loose control on code maintenence and have to start considering integration issues.
new php 5 update is giving me and my client a huge problem suhc as using fuctions preg etc. If possible wish to avoid using other third party. Comet looks like anther frame work and it actually invokes another server...
I will read some more and see if I could use that before I start. That's why the reason I gave up using Jappet to develop this application. let me get back to this later
But thanks for the idea. It;s very useful n I learn something new/
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 12:40 pm
by jackpf
I have actually written my own chat type thingy on my site....
http://jackpf.co.uk/index.php?action=misc&status=im. (click "new" at top right to start a new chat)
I do it using AJAX, and store the messages in a database....then retrieve all messages that are newer than the last one the user received.
This means that it's only selecting at most a couple of rows every five seconds or so for each user....it puts no load on the server at all.
It's pretty basic, but they're the fundamental ideas behind a chat room

Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 3:04 pm
by morris520
to jackpf
That sounds excatly what I am planning to do. I am looking forward to that.
.....
Just been there and played around. got some questions to ask:
1 - functionality question: when i login, it shows i login to IM. does it show if someone else is login if there is any? So it's a chat room right? any one can see other's words...
2 - Tech questions: if it only loads the newer rows are you able to load previous messages if you leave and come back? It looks not now. Does it give heavy load if you load all?
anyway your site is fantastic and well-built. Is it a personal site of you?
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 3:47 pm
by SeaJones
Whatever you do, working with a stateless protocol you'll have to use a fair bit of server load, because of the nature of using a 'Pull' instead of 'Push' technology. A more standard approach would be to use Flash Communication Server, but that's an altogether different approach.
If you do this using PHP and Ajax, then your best areas to optimise it are the SQL queries and the refresh rate. A refresh of once every 250ms compared to 500ms probably wouldn't make much difference to a user, but may well make all the difference to a server if the system was busy.
Re: php ajax chat room will increase server load?
Posted: Sat Jul 04, 2009 5:52 pm
by morris520
to SeaJones
Flash com server wouldn't be my choice so you are right. query optimisation would be a big concern, but I think there's not much to be optimised because the database and table are very simple. Simple 'inserting' and 'selecting' with just a few conditions are very fast.
Thanks for reminding me think which I didn't think about from the ground. Also I think could do something better on the front page to make users not feel the gap.
Your comments are valuable to me. Thanks
Re: php ajax chat room will increase server load?
Posted: Sun Jul 05, 2009 3:37 am
by jackpf
At the moment anyone can enter a chat...but it does give notifications as to who's entered the chat (and who leaves, but only in IE as mozilla browsers don't support window.onunload).
I still have a bit of work to do, but it works
But you can see all my source...that's pretty much it. One giant AJAX() function that handles the reloading of the div, and posting of new messages. I decided to check for new messages every 2 seconds...it's fast enough.
And my bandwidth has not increased at all...and I haven't noticed any lag on my site or anything. But then again, not many people use my site...but still, when you think about it, since it's only fetching new messages, that's only a maximum of like, 2 or 3 each request (every 2 seconds).
And yeah, if you leave the chat and come back, all the old messages will be gone...unless you delete your cookie with the id of the last message in, which will force it to show the whole chat.
And thanks. Yeah, I wrote the entire site myself.
Good luck

Feel free to use my source as a starting point.If you get stuckon anything in particular I'd be happy to help.
Edit:
Oh, and to answer your question about loading all messages...well, loading a large chat (say with around 200 messages or so) every 2 seconds would probably be a bit strenuous on the server...especially if a lot of people are in the same chat, so it would have to load the whole chat, every two seconds, for every person. So potentially thousands of rows every two seconds for each request would definitely be a lot slower than just loading new messages, which would be inthe tens.
Also, I originally did this myself, but I had a problem of scrolling the div to the bottom, to show the new messages. Because it would reload the div every time, the div would scroll up and down every two seconds. Honestly, loading new messages is the only way to go tbh...

Re: php ajax chat room will increase server load?
Posted: Sun Jul 05, 2009 8:10 am
by SeaJones
morris520 wrote:to SeaJones
Flash com server wouldn't be my choice so you are right. query optimisation would be a big concern, but I think there's not much to be optimised because the database and table are very simple. Simple 'inserting' and 'selecting' with just a few conditions are very fast.
Thanks for reminding me think which I didn't think about from the ground. Also I think could do something better on the front page to make users not feel the gap.
Your comments are valuable to me. Thanks
With SQL, another thing you may want to look at closely is the configuration of the server that is running the database. Max queries per minute can be set on some servers, and helps make sure the box doesn't get too stressed and give up.
Re: php ajax chat room will increase server load?
Posted: Sun Jul 05, 2009 8:16 am
by morris520
to jackpf
I think I need to make it for IE and FF. so would think about how to do that when it comes to work. But it looks like a big issue if window.on/unload not works.
I will certainly take your advice to refresh every 2 sec and load only newer messages, I can't afford more server loads, that's the point of my thread. Hah, but to solve the problem for retriving historical message, I can load them only for the first time. And if they don't close the browser I can think about using cookie or cache files. Is this good way to go?
How's your database set up? Are you using single table to store messages?
Re: php ajax chat room will increase server load?
Posted: Sun Jul 05, 2009 6:01 pm
by JAB Creations
A suggestion...flat file. If you are going to INSERT to the database for new messages you might as well do update the last date/time stamp in the file...or use some sort of reference (last modified FAT description in example)..whatever. The idea is that when you contact the server via AJAX you'll want to send the last date/time stamp...and if it no longer matches
then make a database query for the messages that the user has yet to read in the chat room. That'll kill off all the unnecessary SELECT queries that will return no new results for messages.
I'm not sure what else to suggest though I'm going to start following the discussion a little more.
