pub struct Streamed { /* private fields */ }Expand description
A response sent chunk by chunk as it’s made, such as an AI model’s answer as it arrives. It
works with the useStream hooks in @laravel/stream-react and @laravel/stream-vue.
use laravel::http::Streamed;
async fn answer() -> Streamed {
Streamed::channel(|sender| async move {
while let Some(token) = next_token().await {
sender.send(token).await?;
}
Ok(())
})
}The response is text/plain unless you set a content_type. It
tells proxies such as nginx not to buffer it, so each chunk reaches the client as soon as it’s
sent. The framework’s middleware, including the web group’s, pass the response through
without buffering it.
The stream runs in the request’s context, so helpers such as view() and url::route work
inside it, and its logs are tagged with the request. If the stream panics, Laravel reports the
panic and ends the response early.
Implementations§
Source§impl Streamed
impl Streamed
Send a cookie with this response. A (name, value) pair uses the default attributes,
and a Cookie sets its own. The cookie replaces any cookie
that this response already sends with the same name, path, and domain.
use laravel::http::{Redirect, Response};
let response = Response::new("Saved").with_cookie(("theme", "dark"));
let redirect = Redirect::to("/").with_cookie(("theme", "dark"));Remove a browser cookie by sending an expired cookie. A name uses the default path and
domain. To match a custom path or domain, pass a Cookie. The
cookie values that the current request returns stay unchanged.
use laravel::http::Response;
use laravel::cookie::Cookie;
let response = Response::new("Signed out")
.without_cookie("remember")
.without_cookie(Cookie::new("tour", "").path("/admin"));Sourcepub fn with_header<K, V>(self, name: K, value: V) -> Streamed
pub fn with_header<K, V>(self, name: K, value: V) -> Streamed
Send a header with the response, replacing any header with the same name, including one the response would otherwise send:
use laravel::view::{View, view};
async fn report() -> View {
view("reports.show", ()).with_header("cache-control", "no-store")
}A name or value that isn’t a valid header turns the response into a 500 error when
the response is sent.
Sourcepub fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> Streamed
pub fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Streamed
Send several headers with the response, such as
.with_headers([("x-one", "1"), ("x-two", "2")]). Each header replaces any header
with the same name, so when the list holds a name twice, the last value wins.
A name or value that isn’t a valid header turns the response into a 500 error when
the response is sent.
Sourcepub fn new<S>(chunks: S) -> Streamed
pub fn new<S>(chunks: S) -> Streamed
Send a stream’s chunks as they arrive. For a producer that can fail, or that needs to know
when the client disconnects, use channel.
Sourcepub fn channel<F, Fut>(produce: F) -> Streamed
pub fn channel<F, Fut>(produce: F) -> Streamed
Send each chunk a producer sends, as soon as it sends it. The producer runs as its own task
in the request’s context, so helpers such as view() work inside it. The task starts when
Laravel builds the response from this value.
Sender::send fails once the client disconnects, so ? stops the producer without
reporting anything. When the producer returns any other error, or panics, Laravel
reports it and ends the response early, so the client can tell that
the response was cut short.
Sourcepub fn content_type(self, content_type: impl Into<String>) -> Streamed
pub fn content_type(self, content_type: impl Into<String>) -> Streamed
Send this content type, such as text/markdown, instead of text/plain; charset=utf-8. A
content type that isn’t a valid header value turns the response into a 500.
Trait Implementations§
Source§impl IntoResponse for Streamed
impl IntoResponse for Streamed
Source§fn with_header<K, V>(self, name: K, value: V) -> Response
fn with_header<K, V>(self, name: K, value: V) -> Response
500 error.Source§fn with_headers<K, V>(
self,
headers: impl IntoIterator<Item = (K, V)>,
) -> Response
fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> Response
500 error.Auto Trait Implementations§
impl !RefUnwindSafe for Streamed
impl !Sync for Streamed
impl !UnwindSafe for Streamed
impl Forget for Streamed
impl Freeze for Streamedwhere
*const dyn Body<Error = Error, Data = Bytes> + Send is !null: Freeze,
PhantomData<dyn Body<Error = Error, Data = Bytes> + Send>: Freeze,
*const dyn Future<Output = ()> + Send is !null: Freeze,
PhantomData<dyn Future<Output = ()> + Send>: Freeze,
*const [Pos] is !null: Freeze,
PhantomData<[Pos]>: Freeze,
*const u8 is !null: Freeze,
PhantomData<Bucket<HeaderValue>>: Freeze,
PhantomData<ExtraValue<HeaderValue>>: Freeze,
*const ArcInner<Inner> is !null: Freeze,
PhantomData<ArcInner<Inner>>: Freeze,
impl Move for Streamed
impl Send for Streamed
impl Unpin for Streamed
impl UnsafeUnpin for Streamedwhere
*const dyn Body<Error = Error, Data = Bytes> + Send is !null: UnsafeUnpin,
PhantomData<dyn Body<Error = Error, Data = Bytes> + Send>: UnsafeUnpin,
*const dyn Future<Output = ()> + Send is !null: UnsafeUnpin,
PhantomData<dyn Future<Output = ()> + Send>: UnsafeUnpin,
*const [Pos] is !null: UnsafeUnpin,
PhantomData<[Pos]>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<Bucket<HeaderValue>>: UnsafeUnpin,
PhantomData<ExtraValue<HeaderValue>>: UnsafeUnpin,
*const ArcInner<Inner> is !null: UnsafeUnpin,
PhantomData<ArcInner<Inner>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more