looping chat script
Moderator: General Moderators
looping chat script
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....
I have tried putting the whole script in a "while" loop with the while aspect constant but it doesn't work. Please help....
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.
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
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
"good" scripts have finished by then, all others are busted anywaymax_execution_time = 30 ; Maximum execution time of each script, in seconds
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:
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
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:
- if you use an infinite while loop, make sure the script will be terminated if the client aborts also make sure that the script is allowed to survive the max_execution_time limit as volka pointed out.
Code: Select all
php.ini -> ignore_user_abort || ignore_user_abort()
see http://www.php.net/manual/en/features.c ... ndling.php
you´d want to turn the output buffering off - either useorCode: Select all
flushCode: Select all
ob_implicit_flush
see http://www.php.net/manual/en/ref.outcontrol.php
make sure you send some data even if there is nothing new to display to avoid a browser timeout
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....
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... 