Skip to content

examples: add Server-Sent Events (SSE) example#4032

Open
Sim-hu wants to merge 1 commit intohyperium:masterfrom
Sim-hu:server-sent-events-example
Open

examples: add Server-Sent Events (SSE) example#4032
Sim-hu wants to merge 1 commit intohyperium:masterfrom
Sim-hu:server-sent-events-example

Conversation

@Sim-hu
Copy link

@Sim-hu Sim-hu commented Mar 8, 2026

Summary

  • Adds a new server_sent_events example demonstrating how to implement SSE with hyper
  • Uses StreamBody + futures_util::stream::unfold to stream timestamped JSON events
  • Follows the proper SSE wire format (text/event-stream, id, event, data fields)
  • No new dependencies — uses only existing dev-dependencies (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_events
  • cargo run --features="full" --example server_sent_events — tested with curl -N
  • cargo clippy --features="full" --example server_sent_events
  • cargo fmt --check

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant