Search code examples
micronaut

Flux + Server Side Events


Im familiar with Spring Flux + SSE, tried to write some code that stream data.

Two issues here:

  1. When Im making request in Chrome It does not closing connection for a some time (loading circle is spinning), but Im using regular Flux.fromIterable that emmits complete signal at the end of array.
  2. It closes commection for a some time (about 1 minute). Probably its timeout somewhere. Response should be cloused only when OnComplete event fires.

    @Controller("/")
    @Validated
    public class HelloController {
    
        @Produces(MediaType.TEXT_EVENT_STREAM) // add 'application/stream+json'
        @Get("/hello/{name}")
        public Flux<Hello> hello(@NotBlank String name) {
    
            List<Hello> list = new ArrayList<>();
    
            list.add(new Hello("test1", 1));
            list.add(new Hello("test2", 2));
    
            return Flux.fromIterable(list).doOnComplete(() -> {
                System.out.println("response should be closed here!");
            });
        }
    }
    

-Also please add support for the application/stream+json content-type

-And support server side events for a client via Flux.


Solution

  • As far as I am aware the content type application/stream+json is not an official media type. There is a draft attempting to registering this type but it relates to activity streams https://tools.ietf.org/id/draft-snell-activity-streams-type-01.html

    The test https://github.com/micronaut-projects/micronaut-core/commit/b71f63aa8f2b2f00bdcbc25e60d9112321d8f003#diff-29dce542bb2e6326257895b376220c1aR16

    Demonstrates your use case implemented with MediaType.APPLICATION_JSON_STREAM which is of type application/x-json-stream

    As for why chrome keeps spinning this is because Micronaut implements keep alive and keeps the connection alive to satisfy further requests