1
votes

I'm using Spring Webflux, Spring Data, and Project Reactor for non-blocking I/O (Spring Boot 2.0.0.M7).

My goal is to create a stock ticker-like API to allow a client to request all the resources from an endpoint based on some criteria, and also receive new resources that are created after the initial request. Reactive MongoDB is the backing store. The basic HandlerFunction implementation looks like the below.

Mono<ServerResponse> getFoos(ServerRequest request) {
    ok().contentType(TEXT_EVENT_STREAM)
            .body(fooRepository.findAll(), Foo)
}

Obviously, this just returns all the Foos that are currently available, then the Publisher closes the connection and no new Foos are sent to the client. My question is about what pattern to use to add an infinite stream to this which can accept new entries?

  1. Concat with some global Publisher Bean which I write new Foos to as they are created
  2. Add an onComplete which resubscribes to the Repository (with some criteria to filter duplicate entries)
  3. Use repeat and let the client filter the duplicates
  4. Something else?
1

1 Answers

1
votes

If you configure your MongoDB collection accordingly (it has to be capped), you can use tailable cursors to achieve what you want, just add a @Tailable annotation on your repository. See the Spring Data MongoDB reference documentation about infinite streams.