faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

16 of 41 people (39%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

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 1st, 2009 08:42
Sarah Turner, 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 
http://www.aralbalkan.com
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
it.
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
http://www.w3.org/TR/html4 )
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.
http://flyvningertilschweiz.blogtact.com/
http://volssuisse.blogtact.com/
http://volsromeitalie.blogtact.com/
http://viajesvacaciones.blogtact.com/
http://reisenchina.blogtact.com/
http://flugeschweiz.blogtact.com/
http://resasemester.blogtact.com/
http://viaggivacanze.blogtact.com/
http://reisenurlaub.blogtact.com/
http://reizenvakanties.blogtact.com/
http://volisvizzera.blogtact.com/
http://gartenpflege.blogtact.com/
http://geschenkideeen.blogtact.com/
http://vluchtenromeitalie.blogtact.com/