[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

messaging system php, ajax and mysql

Posted on 2014-07-15
9
Medium Priority
?
2,061 Views
Last Modified: 2014-07-23
Hi,

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
0
Comment
Question by:J N
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Author Comment

by:J N
ID: 40198284
if you would like to see what i am talking about the page is:

http://www.bushbrigade.com/bushbase/system/messages.php

you can login using
user: expert-exchange
pass: 3XP3rt!!!!
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 668 total points
ID: 40198352
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.
0
 
LVL 6

Author Comment

by:J N
ID: 40198553
Hi,

what about using websockets? i have done some further readings and it seems websockets are replacing polling. thought?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40199042
Websockets might be a good experiment.  The real, essential, underlying problem is that HTTP is a client/server protocol.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

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.
0
 
LVL 34

Assisted Solution

by:Slick812
Slick812 earned 1332 total points
ID: 40202482
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.
0
 
LVL 6

Author Comment

by:J N
ID: 40202540
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?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 40202584
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.
0
 
LVL 6

Author Comment

by:J N
ID: 40202604
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
0
 
LVL 34

Accepted Solution

by:
Slick812 earned 1332 total points
ID: 40202705
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="http://hacker.com/badjs.js"></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 -
      if(!empty($_SERVER['HTTP_IF_NONE_MATCH']))$b64=$_SERVER['HTTP_IF_NONE_MATCH'];

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,
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Suggested Courses
Course of the Month19 days, 12 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question