I've been wracking my brains for the past few hours trying to figure out
this streaming business and the ways to avoid refreshing and polling the
server for data. I spent a few hours playing Sherlock Holmes in the
phpOpenChat software which is where I first found this technique, but I
didn't succeed.

The most I could find out is that it does indeed use output buffering, but
I'm not quite sure how it's looping the data (most I could find out it was
doing a do {} while() loop with an array called $lines) to display it
without illeffects on the processor. On top of that, I couldn't figure out
how it was printing and checking lines over and over again for a new message
to show in all the other client windows.

So my question is, does anyone know how the phpOpenChat system works? Can
anyone provide some insight into the world of "streaming" and avoiding the
refresh game?

I was also thinking of Javascript like you said, but again, I came back to
refreshing and then I can to XML HTTP Request but it's not supported well
with browsers. Any help here would be appreciated...

 
Thanks,
Stephen Craton
http://www.melchior.us

-----Original Message-----
From: Justin Patrin [mailto:[EMAIL PROTECTED] 
Sent: Thursday, June 24, 2004 5:28 PM
To: Stephen Craton
Cc: PHP List
Subject: Re: [PHP] Streaming

It will use up CPU time and bandwidth, yes. It should take less bandwidth
than the "refresh constantly" version, but only if you code it right.

You could also write a little JS to pull some content from the server (get
messages after a certain time / message id) and inject them into the page.
This is really the best solution if you want to stay in the browser and not
do Flash or Java.

On Thu, 24 Jun 2004 17:24:31 -0500, Stephen Craton <[EMAIL PROTECTED]>
wrote:
> 
> Thanks for all the help here.
> 
> The only question I have left is bandwidth. Would a while(true) { } 
> loop hog up enormous ammounts of bandwidth or just sit there quietly
bugging noone?
> The only other option I have going for me is the iframe hack, which 
> I'm getting annoyed of...
> 
> Thanks,
> Stephen Craton
> http://www.melchior.us
> 
> -----Original Message-----
> From: Justin Patrin [mailto:[EMAIL PROTECTED]
> Sent: Thursday, June 24, 2004 5:01 PM
> To: Stephen Craton
> Cc: PHP List
> Subject: Re: [PHP] Streaming
> 
> Your script is ending, so the page ends. PHP (HTTP) isn't really 
> supposed to do "streaming." If you want it to "stream" you have to 
> keep the script running. Add something like this:
> 
> while(true) {
>        while($db->next_record()) {
>        echo '<b>'.$db->Record["user"].':</b>&nbsp;&nbsp;';
>        $sql = "SELECT emote, image FROM c_emotes";
>        $result = mysql_query($sql, $db->linkid);
>        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
>        $emotes[] = $row['emote'];
>        $images[] = "<img src='".$row['image']."'>";
>        }
>        if($db->Record['system'] != 1) {
>                $message = htmlentities($db->Record["text"]);
>        } else {
>                $message = $db->Record["text"];
>        }
>        $message = str_replace($emotes, $images, $message);
>        echo $message."<br>\n";
>        flush();
>     }
>     sleep(5);
>     set_time_limit(30);
> }
> 
> This will actually run *forever* and will loop through the records 
> again and again, probably giving yu the same content as before. You 
> would need some kind of way to know which records are "new" so you can 
> only display them in the loop. I would suggest adding a "time > 
> $savedTime" or some such to the loop.
> 
> Please note, though, that this is a *very large hack*. You're not 
> supposed to keep your scripts running forever. If you really want to 
> "stream", write a Java/C/C++/C#/etc. app. Or, if you're feeling
adventurous, use JavaScript.
> 
> On Thu, 24 Jun 2004 16:54:57 -0500, Stephen Craton 
> <[EMAIL PROTECTED]>
> wrote:
> >
> > I just put it into my script, and it doesn't seem to be working. I 
> > put ob_end_clean(); at the top of the script, but here's the while 
> > loop I
> have:
> >
> >         while($db->next_record()):
> >         echo '<b>'.$db->Record["user"].':</b>&nbsp;&nbsp;';
> >         $sql = "SELECT emote, image FROM c_emotes";
> >         $result = mysql_query($sql, $db->linkid);
> >         while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
> >         $emotes[] = $row['emote'];
> >         $images[] = "<img src='".$row['image']."'>";
> >         }
> >         if($db->Record['system'] != 1) {
> >                 $message = htmlentities($db->Record["text"]);
> >         } else {
> >                 $message = $db->Record["text"];
> >         }
> >         $message = str_replace($emotes, $images, $message);
> >         echo $message."<br>\n";
> >         flush();
> >         endwhile;
> >
> > It displays just fine if you refresh, but unless you refresh, you 
> > don't see any new data. Any ideas?
> >
> > Thanks,
> > Stephen Craton
> > http://www.melchior.us
> >
> >
> >
> > -----Original Message-----
> > From: Justin Patrin [mailto:[EMAIL PROTECTED]
> > Sent: Thursday, June 24, 2004 4:47 PM
> > To: Stephen Craton
> > Cc: PHP List
> > Subject: Re: [PHP] Streaming
> >
> > ob_end_clean() goes at the top.
> > flush() goes after *every* pice of info you want to show real-time.
> > Basically, after every echo or group of echos.
> >
> > On Thu, 24 Jun 2004 16:43:39 -0500, Stephen Craton 
> > <[EMAIL PROTECTED]>
> > wrote:
> > >
> > > Thanks for this, but how do I use it? Do I just put the
> > > ob_end_clean() at the opening the page I want to stream and then 
> > > put
> flush() at the end?
> > >
> > > Thanks,
> > > Stephen Craton
> > > http://www.melchior.us
> > >
> > >
> > >
> > > -----Original Message-----
> > > From: svk [mailto:[EMAIL PROTECTED]
> > > Sent: Thursday, June 24, 2004 4:25 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: Re: [PHP] Streaming
> > >
> > > On Thu, 2004-06-24 at 16:41, Stephenpp
> > >
> > > To use "streaming", you have to :
> > > Turn off output buffering: ob_end_clean() and flush the output has 
> > > it comes
> > > : flush()
> > >
> > > It is useful to note that :
> > > "Some versions of Microsoft Internet Explorer will only start to 
> > > display the page after they have received 256 bytes of output, so 
> > > you may need to send extra whitespace before flushing to get those 
> > > browsers to
> > display the page.
> > > "
> > >
> > > and
> > > "Pad your output with necessary spaces, wrap your progressing data 
> > > around open (<table>) and end (</table>) tags, and then call 
> > > flush() so that one script will work for Netscape as well.
> > > "
> > >
> > > > I was browsing the net and I found a PHP chat script that 
> > > > claimed that it does not refresh, rather it has "streaming 
> > > > text". This got my interest and was wondering how the script did 
> > > > it. I downloaded it's source code but couldn't find anything 
> > > > useful in it, very hard to read in my opinion. I did a search on 
> > > > PHP.net for streaming but I couldn't understand much of that, 
> > > > just my noobishness
> I suppose.
> > > >
> > > > How exactly would you go about streaming in PHP anyway? This has 
> > > > my interest now...
> > > >
> > > > Thanks,
> > > > Stephen Craton
> > > > http://www.melchior.us <http://www.melchior.us/>
> > > >
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> > > http://www.php.net/unsub.php
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> > > http://www.php.net/unsub.php
> > >
> > >
> > >
> > >
> > >
> >
> >
> > --
> > paperCrane --Justin Patrin--
> >
> > --
> > PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
> > http://www.php.net/unsub.php
> >
> >
> >
> >
> >
> 
> --
> paperCrane --Justin Patrin--
> 
> --
> PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: 
> http://www.php.net/unsub.php
> 
> 
> !DSPAM:40db52b778403726210510!
> 
> 


--
paperCrane --Justin Patrin--

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit:
http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to