Description
- I have looked for existing issues (including closed) about this
Feature Request provide writer access to SSE event
Motivation
For json SSE event data there's a nice function, but in case you have structured data
in a format different then json you are forced to first serialize into something that AsRef<str>
(
Lines 202 to 217 in 6d78e84
This means that for the datastar
rust sdk crate we are forcing users of axum to double allocate or else roll our own SSE which that crate first did.
Proposal
Somehow expose the buffer to write into it. This could be done in an opaque way. E.g. in pseudo code:
// this writer needs to write a new `data: ` prefix when a newline is getting written into it
struct EventWriter<'a>(&'a mut Buffer);
impl Event {
pub fn data_writer(&mut self) -> EventWriter<'_> {
// 1. do your same checks if data wasn't written yet
// 2.if all good write `data:` prefix and set the state that we have data
// 3.return writer
}
This could then be used even by the json
function.
But even better is that now outside users such as datastar
can write data directly into the buffer,
avoiding the extra data allocations for datastar
.
Alternatives
Roll our own SSE or accept useless allocations for each event sent...