Page 1 of 1
looping chat script
Posted: Tue Aug 27, 2002 5:20 am
by runelore
Hi there, I have a chat system and need the main page that displays the messages to automatically update with new messages... I don't want to refresh the page as this has too many drawbacks. What I want to do is have the php "select" script loop so that it keeps checking the database for new entries..
I have tried putting the whole script in a "while" loop with the while aspect constant but it doesn't work. Please help....
Posted: Tue Aug 27, 2002 5:39 am
by mikeq
You may want to look into Dynamic HTML, in which you can have a 'layer' which is refereshed independently of the rest of your page, I think this is the only way without resorting to Java or the like.
Posted: Tue Aug 27, 2002 5:47 am
by runelore
Thanks, do you mean "Iframes"? I will do that... thanks for the help...
Posted: Tue Aug 27, 2002 6:00 am
by runelore
That doesn't work as I still get problems with the page "refresh". I need the page to remain constant but the code to refresh itself. The problems with the page refreshing is that the mouse pointer starts going crazey and also the screen keeps flickering while it is refreshing.. I just want a script that requests the information from the database infinitely.
Posted: Tue Aug 27, 2002 6:05 am
by mikeq
To be honest I don't know if 'Iframes' are DHTML, I've only ever used 'layers'. I'm sure there was a similar question previously on this forum trying to what you want.
Do a search in these forums.
Posted: Tue Aug 27, 2002 6:12 am
by runelore
I have searched and I can't find anything in looping the php script... thanks for the help anyway....
Posted: Tue Aug 27, 2002 6:40 am
by volka
it's in the nature of http. It transfers complete documents.
If you're strict with the protocol you have to send the data-length before the the contents begins. The data is transfered and done - no more data to receive.
Although the world isn't that strict anymore and browsers try to display everything they get (wether the document is complete or not), sockets stay connected and open and data-chunks are separated automagically there is still no real support for continous send and receive in http.
If you see somehing "streaming" within a browser it's (mostly) a plugin using it's own protocoll to send/receive data and display it.
take a look in your php.ini. You will probably find a line
max_execution_time = 30 ; Maximum execution time of each script, in seconds
"good" scripts have finished by then, all others are busted anyway

Posted: Tue Aug 27, 2002 6:57 am
by mikeq
Don't know if it is what you need but
try this
got this using a search for 'DHTML'
Posted: Tue Aug 27, 2002 7:39 am
by runelore
That's actually very helpful for another part of my script.. thanks... Still the same problem with the php but that is one more thing that I can get done now as well... thanks. again...
Posted: Tue Aug 27, 2002 4:56 pm
by Björn
As some others pointed out before, HTML is a 'stateless' protokol and as such not designed for this streaming things but (as always

there are some roundabout ways to achieve something like this. Nevertheless I believe that it is a bit like walking on thin ice...
1. server push
this is best supported by Moz|Netscape, I don´t know if && how the other 'compatible' browsers handle this.
the setup would roughly look like the way you pointed out:
I think I´ve read somewhere that php gets tired of running after 11:59 hours but then again: chatter should give up way before that

As browsers have their own buffering system, you might have to send additional data to force them to dispaly their buffercontents.
You might encounter problems with client <-> proxy confs and timeouts
Remember that this solution is causing a good amount of server load as you are spawning a child process of apache for each client with a nearly infinite php while loop!
2. client pull (meta refresh or JS)
you´ve tried this already but you could avoid the flikering by using a 'hidden' frame in which the data is loaded. Use JS to retrieve the data from the hidden frame and display it in your message box. For the JS part I would go for the DOM methods of the newer browsers which make it much easier to change content dynamically.
Hope this helps a bit
Björn
thank you....
Posted: Wed Aug 28, 2002 5:05 am
by runelore
Hi, and thanks for the comments they were really helpful... I will try using the javascript again, I am not sure what you meant by the DOM section about javascript but I will learn... Thanks again, I like the jhidden frame idea... how would that work? Not completely understood that...

Posted: Wed Aug 28, 2002 5:09 am
by mikeq
DOM = Document Object Model (defines how you refer to objects/properties within an HTML document, IE and Netscape have different DOM I believe)
Posted: Wed Aug 28, 2002 5:28 am
by runelore
Thanks again, I will go away, and try to work some magic now to get it working.... thanks again...