I'm using a LAMP setup with a Flash interface. How do I set up a chat using PHP that updates all the browsers viewing the chat page?
Mar 15th, 2008 21:24
dman, ha mo, Aral Balkan, Paul Kenneth Egell-Johnsen, Seth C, http://www.ttnr.org
The best way to set up a chat / multi-user game with Flash is to use a
java servlet that will broadcast to all connected broswers running the
movie. You can use an XML Socket connection and a XML socket server
like the Fortress Server (http://www.xadra.com/)
I've got a very simple example of how what you're trying to do can be
achieved *without* using a socket server. It is a simple prototype for
a system what would allow synchronized Flash presentations running on
different computers. Here's how it works:
One movie acts as the leader and writes the frame that it's on to a
file. The other movies read that value and go to that frame. Simple as
that. You can download the source code and play with it at
So how does this apply to an online chat? Well, using a similar
technique, you could read/write the entered text into a file that all
connected browsers access. You must make sure that you lock the file
during writes though since more than one client will be accessing it.
Alternately, you could use a mySQL database to store the entered lines,
purging lines after a set number of seconds.
I don't know how much this will tax your server though (probably a lot
if more than a few people are connected) and that's why it's best to go
with an XML server and a socket connection.
If you were trying to do this in HTML then read on:
HTTP is a stateless protocol. When a page is requested from the server
PHP receives some info from Apache, and based on that produces a page
which Apache serves (PHP is run once, then dies). Apache has received
that request from a browser and when the request is served there is no
link between Apache and the browser.
That is as it should be, that is how the web works. Thus there is no way
for PHP to later (since it has already died, and since there isn't a way
to find the browser again) update a page for browsers already viewing
Instead you COULD use meta tags and timeout:
<http-equiv="refresh" content="seconds, url">
(this has to be done in the head of the html document (see
If you add a time of 10 seconds and a the same url, then the script will
be run once more.
Perhaps you should try a Java chat client? Or you could take a look at
http://phpwizard.net/ where they have a package called phpChat, which
would probably show you some tricks which you might use.