Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom SSE event #2415

Open
1 task done
linrium opened this issue Dec 9, 2023 · 0 comments
Open
1 task done

Support custom SSE event #2415

linrium opened this issue Dec 9, 2023 · 0 comments
Labels
A-axum C-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Experience needed to fix: Medium / intermediate

Comments

@linrium
Copy link

linrium commented Dec 9, 2023

  • I have looked for existing issues (including closed) about this

Feature Request

Enable advanced customization of the SSE (Server-Sent Events) Event struct by introducing the EventExt trait.

Motivation

In certain use cases, users may require additional customization options for the SSE Event struct in the Axum library. For instance, Vercel AI SDK doesn't follow the standard and only accepts raw text. So it is impossible to use Axum which always includes the key such as data, id, etc.

Proposal

The proposed solution involves introducing a trait, EventExt, with methods for customizing the SSE Event. Users can implement this trait to add their own logic and fields to the event. The trait includes methods for setting the data field and finalizing the event.

pub trait EventExt {
    fn data<T>(mut self, data: T) -> Self
    where
        T: AsRef<str>;

    fn finalize(self) -> Bytes;
}

Users can then implement this trait for the SSE Event struct at the application level to provide their own customizations.

impl EventExt for Event {
    fn data<T>(mut self, data: T) -> Self
    where
        T: AsRef<str>,
    {
        // Custom logic to handle data customization
        self.data = data.as_ref().to_string();
        self
    }

    fn finalize(self) -> Bytes {
        // Custom logic for finalizing the event
        // Convert the event to Bytes and return
        // ...
    }
}

fn handle_event() -> Sse<impl Stream<Item = Result<TextEvent, Infallible>>, TextEvent> {
  ...
   yield Ok(TextEvent::default().data(content));
  ...
}

This approach allows users to have fine-grained control over the SSE Event without modifying the core Axum library.

I also tried to implement in in this branch https://github.com/linrium/axum/tree/feat/support-custom-sse-event

Alternatives

@davidpdrsn davidpdrsn added C-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Experience needed to fix: Medium / intermediate A-axum labels Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-axum C-feature-request Category: A feature request, i.e: not implemented / a PR. E-medium Call for participation: Experience needed to fix: Medium / intermediate
Projects
None yet
Development

No branches or pull requests

2 participants