looping chat script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

looping chat script

Post 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....
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

Thanks, do you mean "Iframes"? I will do that... thanks for the help...
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post 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.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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.
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

I have searched and I can't find anything in looping the php script... thanks for the help anyway....
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post by mikeq »

Don't know if it is what you need but

try this

got this using a search for 'DHTML'
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post 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...
Björn
Forum Newbie
Posts: 2
Joined: Tue Aug 27, 2002 4:56 pm
Location: Münster, Germany

Post 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
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

thank you....

Post 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... :lol:
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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)
runelore
Forum Commoner
Posts: 25
Joined: Thu Aug 22, 2002 10:14 am

Post by runelore »

Thanks again, I will go away, and try to work some magic now to get it working.... thanks again...
Post Reply