Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

HELP PLZZZZ - AJAX, PHP, Javascript Live Chat - Problem is how to know other session

HELP PLZZZZ - AJAX, PHP, Javascript Live Chat - Problem is how to know othe

4:35 pm on May 8, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:May 30, 2009
posts: 71
votes: 0


Basically I am trying to create AJAX, PHP and Javascript supported live chat.

I am having a problem with the below:
When user write something in a textbox and sends it then this text is added to DB using AJAX and then shown on his screen using AJAX return as it is too much easy but problem is that how I will let the Admin know that any new message is received, well the admin is also connected to chat but I can update a user's screen on the event of INSERTING A MESSAGE TO DATABASE but how could I know the admin of this message. I have following idea in my mind to let the admin knows about it.

I will use javascript to run a simple AJAX script every second. That AJAX script will get the record from database and update it to the ADMIN's screen but problem is here that I read some where that contacting to database many times in a script will cause to much server load, so if I contact my database every second then it will be very harmful for my site. I have read this somewhere but I did not have self experience of this so if anyone of you can guide me then I will be very thankful to him.

I am waiting for your helpful replies.
Thanks in Advance.
5:35 pm on May 9, 2011 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member jab_creations is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Aug 26, 2004
votes: 22

When you're querying to see if there are new messages in the chat room from others you'll want to have the most recent row id that the client has. So if the client has message number 9001 you'll want to AJAX request to the server with the id 9001 and in PHP query if there are any messages from row 9002 or greater.

If you're using junk like innerHTML (does not properly register like anything in the DOM) you're not going to be able to reliably determine the row id's from AJAX loaded content so make sure you stick to using DOM based methods only.

Here is a great resource for working with the DOM...
http://www.howtocreate.co.uk/tutorials/javascript/domintroduction [howtocreate.co.uk]

- John