I want to learn how to make one of these systems from scratch and I'm find a lot of junk links on Google. I really just want a simple tutorial for the most basic PHP and MySQL chat so I can understand the concept before I start messing with jQuery/AJAX.
-
2I think this question is a bit vague - maybe if you focussed it a bit more on the particular aspect you're concerned with it would help. You don't say what your level of expertise with PHP, database design etc is, so what level of tutorial are you looking for? – Polsonby Jan 05 '11 at 09:34
-
1frames + `meta-refresh` was the way before AJAX era – Your Common Sense Jan 05 '11 at 09:35
-
What a vague question. You might have more luck if you ask for help on a specific part of your project, rather than just asking for teaching on the whole thing. – Lightness Races in Orbit Jan 05 '11 at 09:43
-
you like this ? http://stackoverflow.com/questions/4174521/how-to-implement-a-chat-room-using-jquery-php – ajreal Jan 05 '11 at 09:44
-
possible duplicate of [Where can I get a PHP / MYSQL chat room application](http://stackoverflow.com/questions/1230619/where-can-i-get-a-php-mysql-chat-room-application) – Gordon Jan 05 '11 at 09:48
5 Answers
PHP/MySQL chat 101:
1) user opens a browser
2) user enters address in brower
3) browser sends HTTP request
4) server recieves HTTP request
5) server tells PHP interpreter to run PHP script
6) PHP script connects to MySQL database
7) PHP script retrieves list of messages
8) PHP generates HTTP response made of HTML code with messages and form
9) Server sends HTTP response to browser
10) Browser draws HTML from HTTP response
11) User types new message and submits the form
12) Browser send HTTP POST request
13) ...
Found very interesting tutorial here

- 1,049
- 1
- 9
- 17
-
1Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Xavi López Dec 17 '13 at 10:36
A very simple starting point
Have a database table for a Message
id | user | timestamp | message
And have a PHP page that sends an AJAX request to read any new messages.
This will involve checking the database to see if there are any messages since the time the request was received. If no messages, then loop, wait and try again in 100ms (or whatever you think is acceptable lag).
When the Ajax request returns a message (a JSON response would be best), output the user, time and message to the page using JQuery.

- 54,176
- 10
- 96
- 129
-
i just wrote my first ajax app, so still very new to this. Is it acceptable to do polling from browser? Will it not cause high cpu utilization etc? – zaidwaqi Jan 05 '11 at 10:49
-
You can do a poll from the browser, and let the server loop until a new message is found, and then return a response. This is something called long polling, and is one of the few ways to get around the need for sockets in web programming – Codemwnci Jan 05 '11 at 10:51
The live part of your chat is the tricky part, if you are just beginning i would skip that. Start by building a simple guestbook, and then add more features.
There are many tutorials available on how to build a guestbook, and even some free scripts where you can learn from.
After you got your guestbook working, you could add features like auto-loading new messages to make it appear as live, using AJAX polling. What you basically do make an AJAX call to the server at a regular interval to get all the messages and display it on your page.

- 12,403
- 4
- 37
- 46
-
But what about the operator part? There must be an operator (admin) to handle the incoming customer messages and respond to them, right? So, if i understand it correctly, the script at admin side will keep checking the latest entries in database (at regular intervals, say every 10 sec, using ajax) and show to the admin, the details of the message and the corresponding customer. So the admin can select the customer and send a response to him. Then at the customer end, we have to show the messages targeted to that customer (again at regular intervals). – shasi kanth Aug 16 '11 at 12:24
If you must use php and mySQL for chat, atleast have a seperate table for unread messages. If you poll you will most likely need to check the database for new messages every 100ms or so. If your total message table is 1000 rows checking every 100ms will kill your server (especially if many users are connected). I would structure my mySQL database with a table for only unread messages and move them to a bigger table for old messages once read. That way your not checking a big table all the time.
Even better is to use a cache database for unread messages like redis (facebook used memcacheD).
Even even better is to just not use php all together and use an event driven language with callbacks like node.js

- 752
- 10
- 24