Is it possible to stream html? #2696
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
This is possible, and your code on the Rocket side is "correct" in that it indeed streams instances of HTML that change every tick, but your code on the HTMX doesn't do what you seem to think it does. The hx-get attribute fetches data at the referenced URL but doesn't do anything special with streaming data. It doesn't update the target HTML. So, you'd need to use a different part of HTMX. Here's the relevant documentation: https://htmx.org/extensions/server-sent-events/. And you'd need to use SSE for this, which is this in Rocket: https://api.rocket.rs/v0.5/rocket/response/stream/struct.EventStream.html. |
Beta Was this translation helpful? Give feedback.
-
With your help I have found an answer which works: #[get("/events")]
fn events() -> EventStream![] {
EventStream! {
let mut count = 0;
let mut interval = time::interval(Duration::from_secs(1));
loop {
let output = format!(
r#"<progress max="{}" value="{}">{}%</progress>"#,
100, count, count
);
yield Event::data(output.clone()).id("message");
interval.tick().await;
count += 1;
if count == 100 {
break;
}
}
}
} And the corresponding html: <div hx-ext="sse" sse-connect="/events" sse-swap="message">
Contents of this box will be updated in real time
with every SSE message received from the chatroom.
</div> Thank you for quick answers and the rocket framework! |
Beta Was this translation helpful? Give feedback.
With your help I have found an answer which works:
And the corresponding html: