Streaming HTML
Streaming HTML can be used to output HTML content to a user incrementally. This is in particular useful for pages for pages with longer response time, to inform during processing about the progress of the tasks.
Newer OpenACS versions come with templates for the standard themes for streaming pages. The template for streaming pages can be retrieved for the current theme via the API call "template::streaming_template". An application developer should structure streaming HTML pages as follows:
Output top of page:
set context ... set title ... ad_return_top_of_page [ad_parse_template \ -params [list context title] \ [template::streaming_template]]
Output HTML incrementally:
# Assume, we are collecting HTML in the Tcl variable HTML. # Send this HTML chunk incrementally to the user ns_write [lang::util::localize $HTML]
Flush Output from body scripts:
(e.g. template::add_body_script, template::add_event_listener, template::add_body_handler, template::add_script)
ns_write [template::collect_body_scripts]
End of Page:
# Optionally ns_write [lang::util::localize [template::get_footer_html]] ns_write [template::collect_body_scripts]
Full sample script:
Putting everything together
set title "Sample HTML streaming page" set context [list $title] ad_return_top_of_page [ad_parse_template \ -params [list context title] \ [template::streaming_template]] ns_write "<ul>\n" foreach i {1 2 3 4} { set HTML "<li>finish $i: ...</li>\n" ns_write [lang::util::localize $HTML] ns_write [template::collect_body_scripts] ns_sleep 1s } ns_write "</ul>\n" ns_write "<p>Done.</p>" ns_write [lang::util::localize [template::get_footer_html]] ns_write [template::collect_body_scripts]
See this sample script in action: https://openacs.org/streaming
Caveat: Windows PCs having the current (Sept 2020) version of Bitdefender (antivirus software ) installed with HTTP traffic scanning activated don't show the incremental rendering of the page, but just the full page after it has finished. This might be seen as a problem of Bitdefender and affects streaming HTML from all sites.