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

Setting an event type with poem_openapi::payload::EventStream? #378

Closed
bryanlarsen opened this issue Aug 29, 2022 · 2 comments
Closed

Setting an event type with poem_openapi::payload::EventStream? #378

bryanlarsen opened this issue Aug 29, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@bryanlarsen
Copy link

poem::web::sse::Event gives a mechanism (event_type()) for adding an event type to an sse event.

poem_openapi::payload::EventStream does not, at least as far as I can tell. Is this possible?

@bryanlarsen bryanlarsen added the enhancement New feature or request label Aug 29, 2022
sunli829 added a commit that referenced this issue Aug 30, 2022
@sunli829
Copy link
Collaborator

sunli829 commented Aug 30, 2022

Here is the example to show how to convert the message to SSE event:

pub fn to_event(self, f: impl FnMut(T::Item) -> Event + Send + 'static) -> Self {

@bryanlarsen
Copy link
Author

Awesome! 2.0.11 isn't on cargo yet, so I switched to git to try it out.

I was working from your poem_openapi sse example, so I ended up with something like this:

    #[oai(path = "/foo", method = "get")]
    async fn foo(&self) -> EventStream<BoxStream<'static, serde_json::Value>> {
        EventStream::new(
            async_stream::stream! {
                for i in 0.. {
                    tokio::time::sleep(Duration::from_secs(1)).await;
                    yield serde_json::json!({"a": 3, "b": i});
                }
            }
            .boxed(),
        )
        .to_event(|ev| Event::message(ev.to_string()).event_type("foo"))
    }

Feel free to put this example into your repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants