Streaming
An action writes HTML into the page as it runs, and the response still ends with the ordinary snapshot and effects.
#[component]
impl Streamer {
pub fn run(&mut self, context: &mut ComponentContext) {
for line in ["one", "two", "three"] {
context.stream("log", format!("<div data-line>{line}</div>"));
self.lines.push(line.to_owned());
}
}
}Run the action and watch the lines arrive as frames before the final snapshot finishes the response.
A streamed response is the normal update response with frames in front of it. The server collects what the action streamed, writes each frame, then writes the payload a plain answer would have carried. The client reads the frames while the body arrives and parses the payload as usual, so nothing about the snapshot or the protocol changes: streaming is a transport choice.
The helper
Take a ComponentContext in the action and call stream. The name selects the element to write into.
#[component]
impl Streamer {
pub fn run(&mut self, context: &mut ComponentContext) {
for line in ["one", "two", "three"] {
context.stream("log", format!("<div data-line>{line}</div>"));
self.lines.push(line.to_owned());
}
}
}
<div wire:stream="log">
{% for line in lines %}<div data-line>{{ line }}</div>{% endfor %}
</div>
Each call produces one frame, in the order the action made the calls. The client appends the content to the end of the element marked wire:stream="log". An element marked wire:stream.replace="log" replaces its contents instead. If the component holds no element with that name, the frame is read and dropped.
Frames belong to actions. A component that streams during mount has nowhere to send them: only the frames an update collects reach the response.
The frame
One frame is one JSON object with three keys, written with no separator between frames:
{"stream":true,"body":{"id":"kOa7wQ2cJv9tLbX1mR4d","type":"directive","name":"log","content":"<div data-line>one</div>"},"endStream":true}
| Key | In the frame | In the body |
|---|---|---|
stream | Always true | |
body | The chunk | |
endStream | Always true | |
id | The component's instance ID | |
type | directive, which selects by wire:stream name | |
name | The name the action passed | |
content | The HTML to write |
Upstream also puts mode, el, and ref in the body, for its element and ref targets. This port sends only the four body keys above, which is the directive form the client needs for wire:stream.
The client reads a stream because of a response header, not because of the content type. It pulls every object that matches {"stream":true through "endStream":true} out of the arriving text, hands each body to the stream handler, and keeps the rest as the payload. That means frames must be concatenated exactly this way: one object after another, nothing between them, and the payload last.
The response headers
| Header | Value |
|---|---|
content-type | text/event-stream |
cache-control | no-cache |
x-livewire-stream | true |
Upstream also sends X-Accel-Buffering: no so a proxy in front of PHP does not hold the body back. This port does not send it, because the body is not chunked: the handler buffers the frames and the payload and sends them in one response. A proxy therefore has nothing to flush early, and the header would claim an ordering the server does not provide.
Where the server decides
An update handles every component in the batch first. Each component's action collects its frames into the request's state; the handler then joins them in component order, appends the payload, and only then picks the headers:
let (bytes, streamed) = if frames.is_empty() {
(payload, false)
} else {
let mut bytes = Vec::with_capacity(payload.len());
for frame in &frames {
match serde_json::to_vec(frame) {
Ok(frame) => bytes.extend(frame),
Err(_) => return empty(StatusCode::INTERNAL_SERVER_ERROR),
}
}
bytes.extend(payload);
(bytes, true)
};
Two consequences follow. One component with frames makes the whole batch a stream, and a batch with no frames is a plain JSON response with none of the three headers. And because the decision is taken after the renders, a guard that refuses an update, or an error, still answers with its own status and body: no frame was written before the refusal was known.
Asset injection also leaves a stream alone. The middleware that injects the runtime into a page runs only on responses whose content type contains text/html; a stream is text/event-stream, so its body is never buffered or rewritten.
A worked example
The component below streams three lines and keeps them. Its view has a wire:stream="log" element and a button that calls run.
#[derive(Default, LivewireComponent)]
#[livewire(view = "livewire.streamer")]
pub struct Streamer {
pub lines: Vec<String>,
}
#[component]
impl Streamer {
pub fn run(&mut self, context: &mut ComponentContext) {
for line in ["one", "two", "three"] {
context.stream("log", format!("<div data-line>{line}</div>"));
self.lines.push(line.to_owned());
}
}
}
The browser posts one call, {"method":"run","params":[],"metadata":{}}. The response is 200 with the three headers above, and its body is these four JSON objects, one after another, with no whitespace between them and with a shortened snapshot here. The line breaks below are for reading only:
{"stream":true,"body":{"id":"kOa7wQ2cJv9tLbX1mR4d","type":"directive","name":"log","content":"<div data-line>one</div>"},"endStream":true}
{"stream":true,"body":{"id":"kOa7wQ2cJv9tLbX1mR4d","type":"directive","name":"log","content":"<div data-line>two</div>"},"endStream":true}
{"stream":true,"body":{"id":"kOa7wQ2cJv9tLbX1mR4d","type":"directive","name":"log","content":"<div data-line>three</div>"},"endStream":true}
{"components":[{"snapshot":"{\"data\":{\"lines\":[\"one\",\"two\",\"three\"]},\"memo\":{...},\"checksum\":\"...\"}","effects":{"returns":[null],"html":"<div class=\"card\" data-streamer>...</div>"}}],"assets":[]}
The client appends each frame's content to the log element as it reads it, then morphs the final effects.html over the component. The snapshot in the last object holds lines, so the element the morph produces shows all three lines again. The browser's DOM is replaced, not appended twice.
Appended content does not survive the payload. The final markup replaces the component's element, so anything a frame added is gone unless the component's own render includes it. That is why the example pushes each line into a field: the stream shows progress while the action runs, and the render makes it stay.