pub struct EventStream { /* private fields */ }Expand description
A stream of server-sent events, sent as a response.
use laravel::http::sse::{Event, EventStream};
use laravel::stream::{self, Stream, StreamExt};
use laravel::view::{data, view};
async fn stream() -> EventStream {
EventStream::new(added_tasks().map(|task| {
Event::new("task-added", view("tasks.index", data! { task }).fragment("row"))
}))
}Data sent without a name becomes an update event. The stream ends with an update event
whose data is </stream>, which the @laravel/stream hooks listen for:
use laravel::http::Json;
use laravel::http::sse::EventStream;
use laravel::stream::{self, StreamExt};
async fn progress() -> EventStream {
// Sends `update` events of `{"done":1}`, `{"done":2}`, and `{"done":3}`...
let events = stream::iter(1..=3).map(|done| Json(serde_json::json!({ "done": done })));
EventStream::new(events)
}Each view renders as its event is sent. When a view fails to render, or JSON can’t be serialized, Laravel reports the error and skips the event. By then the response has already begun, so it can’t become an error page.
On a quiet stream, the response sends a comment every 15 seconds so proxies don’t close the connection. The stream ends when the server begins to shut down, so it doesn’t hold up a deploy. In that case, the stream ends without its end event, so browsers reconnect to the new server.
Implementations§
Source§impl EventStream
impl EventStream
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) -> EventStream
pub fn with_header<K, V>(self, name: K, value: V) -> EventStream
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)>,
) -> EventStream
pub fn with_headers<K, V>( self, headers: impl IntoIterator<Item = (K, V)>, ) -> EventStream
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<E>(events: impl Stream<Item = E> + Send + 'static) -> EventStream
pub fn new<E>(events: impl Stream<Item = E> + Send + 'static) -> EventStream
Send the stream’s events. Each item is an Event, or data for an update event.
Sourcepub fn channel<F, Fut>(produce: F) -> EventStream
pub fn channel<F, Fut>(produce: F) -> EventStream
Send each event 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 and errors propagate
with ?. The task starts when Laravel builds the response from this value.
Sender::send fails with ClientGone 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 stream early without sending the end
event.
use laravel::http::sse::{Event, EventStream};
async fn progress() -> EventStream {
EventStream::channel(async |sse| {
sse.send("Starting").await?;
sse.send(Event::new("done", "Finished")).await?;
Ok(())
})
}Sourcepub fn end_with(self, event: impl Into<Event>) -> EventStream
pub fn end_with(self, event: impl Into<Event>) -> EventStream
End the stream with this event, instead of an update event of </stream>.
Sourcepub fn without_end_event(self) -> EventStream
pub fn without_end_event(self) -> EventStream
End the stream without an end event.
Sourcepub fn keep_alive(
self,
every: impl Into<ZonedDateTimeArithmetic>,
) -> EventStream
pub fn keep_alive( self, every: impl Into<ZonedDateTimeArithmetic>, ) -> EventStream
Send a comment at this interval on a quiet stream, such as 30.seconds(), so proxies
don’t close the connection. The default is every 15 seconds.
Trait Implementations§
Source§impl Debug for EventStream
impl Debug for EventStream
Source§impl IntoResponse for EventStream
impl IntoResponse for EventStream
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 EventStream
impl !Sync for EventStream
impl !UnwindSafe for EventStream
impl Forget for EventStream
impl Freeze for EventStreamwhere
*const dyn Stream<Item = ChannelItem> + Send is !null: Freeze,
PhantomData<dyn Stream<Item = ChannelItem> + Send>: Freeze,
*const dyn Future<Output = ()> + Send is !null: Freeze,
PhantomData<dyn Future<Output = ()> + Send>: Freeze,
*const u8 is !null: Freeze,
PhantomData<u8>: Freeze,
*const ArcInner<dyn Render> is !null: Freeze,
PhantomData<ArcInner<dyn Render>>: Freeze,
*const [Pos] is !null: Freeze,
PhantomData<[Pos]>: Freeze,
PhantomData<Bucket<HeaderValue>>: Freeze,
PhantomData<ExtraValue<HeaderValue>>: Freeze,
*const ArcInner<Inner> is !null: Freeze,
PhantomData<ArcInner<Inner>>: Freeze,
impl Move for EventStream
impl Send for EventStream
impl Unpin for EventStream
impl UnsafeUnpin for EventStreamwhere
*const dyn Stream<Item = ChannelItem> + Send is !null: UnsafeUnpin,
PhantomData<dyn Stream<Item = ChannelItem> + Send>: UnsafeUnpin,
*const dyn Future<Output = ()> + Send is !null: UnsafeUnpin,
PhantomData<dyn Future<Output = ()> + Send>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const ArcInner<dyn Render> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Render>>: UnsafeUnpin,
*const [Pos] is !null: UnsafeUnpin,
PhantomData<[Pos]>: 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