http://qs321.pair.com?node_id=152200

BioHazard has asked for the wisdom of the Perl Monks concerning the following question:

For a while I've been looking for a possibility to make a real-time web-chat with Perl. Every other implementation I've seen so far, needed to refresh the whole browser frame to display the entered text. How could I realize to "stream" the output to the client without refreshing any frame? I tried using a NPH-Script by setting the special Variable $| = 1; but that doesn't seemed to do the trick.
I've seen this "streaming" in several Java-Chats with HTML-Front-End (no applet!) (i.e. Cassiopeia Software) and I would be surprised if this is not possible with Perl.

Thanks for your help!

Replies are listed 'Best First'.
Re: Real-Time Web-Chat with Perl
by dws (Chancellor) on Mar 16, 2002 at 20:21 UTC
    I've seen this "streaming" in several Java-Chats with HTML-Front-End (no applet!) and I would be surprised if this is not possible with Perl.

    Indeed it is possible. There are two principal schemes for active chat, both of which require JavaScript on the client side. On scheme relies on doing a periodic refresh from a frame to a server CGI that responds with the most recent messages. Posting a new message happens from a different frame.

    The second scheme relies on streaming a sequence of JavaScript commands into a hidden frame. The commands will append to the message stream in a visible frame. Posting a new message happens from a different frame.

    To make either of these schemes work, the server side has to do a certain about a client "capability detection", so that the server can know what flavor of frames and JavaScript to use. If you go either route, you'll end up with templates that have conditional chunks of Netscape or IE-flavored JavaScript.

    merlyn has a column that shows how to implement the former scheme. For details on the latter scheme, see this paper, which talks about the scheme from a Java-centric viewpoint.

      Actually, I had a chat script that worked using the whole $| = 1; thing.

      The problem I ran into and the reason I tossed out the script was that if no one says anything for a long enough period of time, browsers will return an error saying that the document cannot be loaded. I later found out a way to fix this... All I needed to do was make sure I printed out an HTML comment every 30 seconds or so. lol, wish I hadn't tossed it now...

      The way I accomplished the client-side was indeed via JavaScript. I just did a setInterval() for 1000ms (1 second) that executed a scrollBy() statement to scroll the chat window down to the bottom. :)

Re: Real-Time Web-Chat with Perl
by Juerd (Abbot) on Mar 16, 2002 at 18:09 UTC

    How could I realize to "stream" the output to the client without refreshing any frame? I tried using a NPH-Script by setting the special Variable $| = 1; but that doesn't seemed to do the trick.

    Most browsers start rendering after they have received 512 bytes.
    A while ago, I created a web-chat that connects to IRC using POE::Component::IRC. (It's in use (as far as I know) on FellaX.net and Rhizomatic.net). Only Konqueror really needs the page to load completely, so it doesn't work in that browser. It does however work perfectly in Netscape 4, MSIE, Mozilla, Opera and many others.

    Example (#foo on irc.convolution): http://webchat.convolution.nl/
    Download: http://webchat.convolution.nl/chataddict.tar.gz

    Warning: I coded this a long time ago, and it's coded quite badly.

    The important piece of code is:

    print '<!--', '*'x512, '-->'
    This is quite embarrassing :)

    HTH </code>

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

Re: Real-Time Web-Chat with Perl
by theguvnor (Chaplain) on Mar 16, 2002 at 18:07 UTC

    What you are doing by setting $|=1 is telling the perl interpreter to autoflush all your output (any monks unfamiliar1 can see here for a thorough treatment) which can be a useful thing but I'm not sure is going to be the whole answer...

    Have you read through the documentation for the CGI module which does tell you how to get started with No-Parsed-Header output?

    I wish you good luck!

    ..Guv

    1 Not implying the poster would be unfamiliar with the topic...

Re: Real-Time Web-Chat with Perl
by true (Pilgrim) on Oct 27, 2002 at 20:33 UTC
    Here's another suggestion for you. This only works with javascript ON. Have your html page load a image. Set the img's src to a perl script you write. Add a onLoad javascript tag.
    <html><title>page.html</title> <img src="http://www.myperlscripturl.cgi"> <body onLoad="window.location(page.html');"> </html>
    Have the perl script open an image on your server and spit it out. Before the image-serving perl script dies, put it in a sleeped loop. Every loop the perl script checks your chat log file. When the chat log file has a new modification date, exit the perl script.
    Upon exiting the loop, the javascript will now complete it's body onLoad function. This will give you a chat like webpage which will only refresh the html page when new live content is put on the server.
    - Hope this helps.