messaging system php, ajax and mysql


i am developing a chat system using ajax, php and mysql.

I have gotten to the point where a user can hit post, the ajax is successfully submit, mysql will update and a response is given. everything works fine on that front.  However, the ajax response will only update that users page ( the recipient of the message will have to refresh the page to see the new message). how is it possible to update the entire page so that both users see the new content. i have done some reading and it seems like im looking for long polling or html pushes. Im not very familiar with these so any information or suggestions is appreciated

thanks in advance
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

J NUnicorn wranglerAuthor Commented:
if you would like to see what i am talking about the page is:

you can login using
user: expert-exchange
pass: 3XP3rt!!!!
Ray PaseurCommented:
AJAX and PHP may be the wrong tools; long polling is probably a better idea.  That said, it can be done in AJAX and PHP, but it will probably not scale well.  Here is the theory.

Every client has a script that contains a timer.  When the timer goes off, the script makes a request to the server for the latest "chat" information.  At the server, each such request queries the data base for the most recent updates, and sends the updates to the client (in response to the timer-driven AJAX request).  This is how GMail notifies you that you have a new message.  If the timer fires every second or every two seconds, it will appear to have contemporaneous notifications.
J NUnicorn wranglerAuthor Commented:

what about using websockets? i have done some further readings and it seems websockets are replacing polling. thought?
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Ray PaseurCommented:
Websockets might be a good experiment.  The real, essential, underlying problem is that HTTP is a client/server protocol.

In the HTTP design, the server can only respond after the client initiates communication with a request, and the server can only respond to the single client that initiated the request.  The design of a chat system would seem to imply that the server can initiate communication.  So there has to be some work to program around that limitation.
greetings  M. Jayme Nagy, I have done chat in php using AJAX, and it can be successful for  Small Scale (low user count) chat html apps.  PHP is NOT going to work on a medium or large scale CHAT no matter what you do or how you do the chat. The limit of PHP  simultaneous chat  users is between 250 and 400 users, if you have a high page access on that PHP server then the limit goes down. This is because PHP is NOT a low latency server connection-response set up, as the server transactions in PHP require the PHP processor to  do work (set up the ZEND engine, allocate memory, check POST and GET data and other things) for a HTTP exchange.

ONE thing I will stress here, that making a working PHP chat (small scale) from scratch (no chat framework) needs javascript and php experience, because what seems to  work in your tests (as only one user, you the developer) can seem successful, BUT as soon as several many users go into the chat, and there are chat messages from several users at about the same time, then things may get out of sync or messed up, because your single use test does not factor in necessary differences in the many user "ID" that need to be precisely placed in DB and on the output chat message board.

Your best bet is to use one of the hundreds of PHP chat frameworks that are available, and fully tested and have years of bug fixing.
J NUnicorn wranglerAuthor Commented:
Thanks Slick!

i think my app would be small time. i cannot see 250 -400 users using this services at the same time when we have services such as facebook out there. that being said if it did get to that stage i would like it to be fairly easy to "upgrade" it. i have been steering away from frameworks as im using all of this as learning experience. however, if its a complete waste of time, i am more than willing to try a framework.

on your second point about small difference, what would be a good indication that things are going awry? how could i test this without asking a bunch of people to login and try?

on your third point, do you have any recommendations of which one to use?
Ray PaseurCommented:
A learning experience is never a complete waste of time, even if it does not result in a deployable application.  You will be able to build your teaching library, if nothing else.

A two-way chat, such as those ubiquitous "chat with a representative now" dialog boxes will be a good place to get started.  It will be easier to build and test than a multi-way chat room.  If you decide that you want a multi-way chat room the easiest way to test would be to buy several friends some beer and tell them to bring their laptops.  There are formalized ways to test these kinds of apps, but they require a whole new layer of specialized tools and skills.
J NUnicorn wranglerAuthor Commented:
Thanks Ray!

The chat system i have in mind will only allow users to chat with other members (no guests)

1. either private (me and you) messages
2. or a group message

similar to facebooks messaging without the massive amount of concurrent users
OK,  as to - "what would be a good indication that things are going awry?"
the user GUI for the "users list" and the scrolling <div> for the chat messages display, if a chat post is missing, or assigned to the wrong USER, or incorrect in date-time, or shortened or has HTML entries NOT FILTERED (as if they type in <strong> or <script scr=""></script> ) and a score of other problems (especially if you use Bulletin Board html variants like [n], as experts exchange does) that you will never even think of.

at first, you alone, have three different browsers on your desktop (IE, firefox, chrome, opera) and use the chat as three or four different users that "sign In", "post messages", and "sign out".

as to -"asking a bunch of people to login and try?"
Then in your social media (faceBook) friends, ask them for some time to help you "TEST" your web app.

as to - "on your third point, do you have any recommendations of which one to use? "
I have Not looked at the chat frameworks for PHP in years, you can do a web search for "PHP chat" or "PHP chat class" or "PHP chat framework", at first you should download some of the "code" for the chat class, and look at the ways that they use the AJAX , , and the browser and server code to try and see what they do (like what process do they use if the user does a browser "RELOAD CURRENT PAGE" and you want it to show the message board as if they never changed the page). I did this and learned fantastic amounts about AJAX and the need for super "efficiency" in the minimal data posted and received in the request (ALL OPERATIONS in chat take time and resorces in PHP, so you need to minimize data sent to server, and data returned from server).

ALL of the chat frameworks are different GUI, so some user interface may suit you and some may not.

as something to consider, I found that setting the "HEADERS" in the browser AJAX to have dated values and then getting the headers in PHP as -

and then setting the headers -
      header('ETag: '.$vf64);
      header('Status: 304 Not Modified');

greatly helped me with "PRE-PROCESSING" for server and browser code work.

you will need to do development "testing" to learn the code particulars of AJAX, it is necessary to use AJAX in a comparable and efficient manner,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.