examples: add Server-Sent Events (SSE) example#4032
Open
Sim-hu wants to merge 1 commit intohyperium:masterfrom
Open
examples: add Server-Sent Events (SSE) example#4032Sim-hu wants to merge 1 commit intohyperium:masterfrom
Sim-hu wants to merge 1 commit intohyperium:masterfrom
Conversation
Add an example demonstrating how to implement Server-Sent Events using hyper's streaming response API. The example uses `StreamBody` with `futures_util::stream::unfold` to send timestamped JSON events to connected clients once per second. SSE is a common pattern for real-time web applications, and no existing example covers the `text/event-stream` content type or the SSE wire format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
server_sent_eventsexample demonstrating how to implement SSE with hyperStreamBody+futures_util::stream::unfoldto stream timestamped JSON eventstext/event-stream,id,event,datafields)futures-util,http-body-util)Motivation
Several existing examples show streaming responses (
echo.rs,send_file.rs), but none cover the Server-Sent Events pattern. SSE is widely used for real-time web applications, and implementing it correctly with hyper's low-level API is a common question.Checklist
cargo build --features="full" --example server_sent_eventscargo run --features="full" --example server_sent_events— tested withcurl -Ncargo clippy --features="full" --example server_sent_eventscargo fmt --check