Re: How to stream data in real time using Javascript?



"Charles" <me@xxxxxxxxxxx> wrote in message
news:op.s42txm0bix6gqv@xxxxxxxxxx
Hello, I have a specific need to stream text from a database to a browser
window in real time. Basically, I need this for a chat application such as
phplive. A browser window needs to display in real time new messages that
are stored on a remote database. I have studied how existing solutions
work, and this is not exactly the way I would like to do. Existing
solutions just use a javascript that refreshes every 5 seconds to see if
there is a new message, if so, it displays it in the window.

Is there a nicer way of doing it without the "refresh" hack?
Thanks,

There is a way... oh there is a way. It's heavily browser-dependent, as as
others have pointed out, HTTP isn't really made for this kind of thing.

Here's how it works: Modern browsers will render/interpret HTML chunks as
they're read from the server. If you have some PHP, say, that outputs
chunks of javascript (complete with its own script tags - very important),
as soon as the tag is closed, the browser will run the script. The PHP can
sleep or wait for new data, then start outputting script chunks when the
data is ready. If the connection is dropped, the php script will know
(using the connection-handling functions), and the window with the hidden
iframe containing the PHP URL will know (as the onload event will be fired).
If the iframe is refreshed only when the connection is dropped, you
essentially get streaming from server-side. It's not as efficient as
streaming binary data, but it works, and is reliable.

dave

--
Charles.

Desenvolvimento e criação de sites: www.auriance.com
Hospedagem de sites e servidores dedicados: www.auriance.net


.



Relevant Pages

  • Re: how to run scripts after a page has already loaded and been sent to a users browser?
    ... > It's because PHP is a server side scripting language, ... > do stuff before you send stuff to user's web browser. ... As we've discussed on this newsgroup before, if a script starts ... that is after the last HTML is sent to a web browser. ...
    (alt.php)
  • SP2 and Javascript
    ... Web sites from running scripts that resize or reposition your browser ... As my start page is a script that resizes and repositions my browser window, ...
    (microsoft.public.windowsxp.general)
  • Re: SP2 and Javascript
    ... >pages will display fully in 800x600, so I force my browser to that size, as ... >As my start page is a script that resizes and repositions my browser window, ... >it seems I was correct in thinking that IE would block my attempts to resize ... >that tells me it won't run my script commands. ...
    (microsoft.public.windowsxp.general)
  • PHP Program Randomly Stops
    ... "You can use a combination of PHP with client side code to keep the ... are pages to be processed print a client side script, e.g. JavaScript, ... This will reload until all pages are done - then ... "You are running the script from a browser. ...
    (comp.lang.php)
  • Re: Which Is The Better Approach To Working With Javascript?
    ... Java SCRIPT runs in the browser exclusively. ... PHP can also run in a browser, ...
    (comp.lang.php)