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

Integrate reactive routes and Mutiny #9477

Closed
cescoffier opened this issue May 20, 2020 · 6 comments
Closed

Integrate reactive routes and Mutiny #9477

cescoffier opened this issue May 20, 2020 · 6 comments

Comments

@cescoffier
Copy link
Member

Better integration of reactive routes and mutiny to allow routes to return Uni/Multi.

@murphye
Copy link

murphye commented May 24, 2020

I was playing with this previously. This seems to work, but I am wondering what improvements you have in mind...

    @Route(path = "/multi", methods = HttpMethod.GET)
    void multi(RoutingExchange ex) {
        Multi<String> source = Multi.createFrom().items("a", "b", "c");

        var response = ex.response();
        response.setChunked(true);
        source.onCompletion().invoke(response::end).subscribe().with(response::write);
    }

@murphye
Copy link

murphye commented May 24, 2020

Another example using SSE. This will send messages to all consumers of the /stream endpoint from the EventBus.

    @Route(path = "/stream", methods = HttpMethod.GET)
    void stream(RoutingExchange ex) {
        var response = ex.response();
        response.setChunked(true);
        response.headers().add("Content-Type", "text/event-stream;charset=UTF-8");
        response.headers().add("Connection", "keep-alive");

        eventBus.<String>consumer("sample").toMulti()
               .onItem().apply(m -> m.body())
               .onItem().apply(b -> "data: " + b + "\n\n")
               .subscribe().with(response::write);
    }

@cescoffier
Copy link
Member Author

The idea is to return the Uni and Multi directly and avoid the user-managed subscription.

@murphye
Copy link

murphye commented May 26, 2020

OK. Maybe the functionality could be extended a bit:

  1. Automatically handle subscibe and onCompletion
  2. Automatically handle the onError and return appropriate response (500 error?)
  3. Automatically setChunked(true) for Multi
  4. Automatically add data: and \n\n for optional SSE
  5. Automatically headers().add text/event-stream and keep-alive for optional SSE
  6. Automatically return body() from eventBus.<String>consumer("sample").toMulti() when EventBus is used

@cescoffier
Copy link
Member Author

It's exactly what I had in mind!

@cescoffier
Copy link
Member Author

@mkouba and I have done a first integration - already in master.

@cescoffier cescoffier added this to the 1.7.0 - master milestone Jul 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants