Module sse
Expand description
Server-Sent Events (SSE) support
§Server-Sent Events (SSE)
Server-Sent Events (SSE) is a simple and efficient mechanism for servers to push real-time updates to clients over HTTP. Unlike WebSockets, which provide full-duplex communication, SSE establishes a one-way channel from server to client using a long-lived HTTP connection.
Rama offers support for SSE both as a client and a server.
As such you could even MITM proxy it.
Server IntoResponse support is also supported, including with Keep-Alive support.
See https://ramaproxy.org/docs/rama/http/service/web/response/struct.Sse.html for more info.
Learn more about SSE at https://ramaproxy.org/book/sse.html.
§Examples
You can find ready-to-run examples demonstrating how to expose and consume SSE endpoints using Rama:
http_sse.rsSimple example showing how to expose an SSE endpoint with string data.http_sse_json.rsSame as above, but emits structured JSON data using typed Rust structs.
These examples make use of Rama’s typed header support, such as LastEventId, which allows easy extraction of reconnect state to resume streams reliably.
§Datastar
Datastar helps you build reactive web applications with the simplicity of server-side rendering and the power of a full-stack SPA framework.
Rama has built-in support for 🚀 Datastar. You can see it in action in Examples:
- /examples/http_sse_datastar_hello.rs: SSE Example, showcasing a very simple datastar example, which is supported by rama both on the client as well as the server side.
Rama rust docs:
- SSE support: datastar
- Extractor support (
ReadSignals): https://ramaproxy.org/docs/rama/http/service/web/extract/datastar/index.html - Embedded JS Script: https://ramaproxy.org/docs/rama/http/service/web/response/struct.DatastarScript.html
§Code Origins
Some code in this repo is adapted from third party sources:
- Axum (see https://github.com/plabayo/rama/blob/main/docs/thirdparty/fork/README.md#relative-forks)
- https://github.com/jpopesculian/eventsource-stream/tree/3d46f1c758f9ee4681e9da0427556d24c53f9c01:
- Licensed under MIT OR Apache-2.0, owned by Julian Popescu jpopesculian@gmail.com
Modules§
- datastar
- 🚀 Datastar support for rama.
- server
- SSE types for servers.
Structs§
- Event
- Server-sent event
- Event
Build Error - Event
Data Json Reader EventDataLineReaderfor theEventDataReadimplementation of any json-compatibleDeserializeOwned.- Event
Data Multi Line Reader EventDataLineReaderfor theEventDataReadimplementation ofVec.- Event
Data String Reader EventDataLineReaderfor theEventDataReadimplementation ofString.- Event
Stream - A Stream of SSE’s used by the client.
- Json
Event Data - Wrapper used to create Json event data.
Traits§
- Event
Data Line Reader - Event
Data Read - Trait that can be implemented for a custom data type that is to be read (by a client).
- Event
Data Write - Trait that can be implemented for a custom data type that is to be written (by a server).