Following up from this question: Play Framework 2.5 Streaming content with delay
I have been trying to stream data from MongoDB Reactive through Akka Streams and Play Framework. The problem is that for some reason the data collects first and then sends the complete collection back instead of streaming each item individually.
The Mongo Driver code:
public FindPublisher<Document> findAll(){
FindPublisher iterator = collection.find();
return iterator;
}
Mapping from Mongo Publisher to Source
public Source<Rating, NotUsed> findAll(){
return Source.fromPublisher(mongoConnection.findAll()).map(doc -> new Rating().fromDocument(doc));
}
Return from Play Framework Controller
public Result findAll(){
Source<Rating, NotUsed> ratingsStream = ratingRepository.findAll();
Source<ByteString, NotUsed> byteStringStream = ratingsStream
.map(rating -> ByteString.fromString(rating.toDocument().toJson() + "\n"))
.delay(FiniteDuration.create(100, TimeUnit.MILLISECONDS), DelayOverflowStrategy.backpressure());
HttpEntity.Streamed streamed =
new HttpEntity.Streamed(byteStringStream, Optional.empty(), Optional.of("text/event-stream"));
return ok().sendEntity(streamed);
}
The delay here only delays the whole stream (like initialDelay()) , and then returns the entire Mongo data.
Following from my previous question this method works when creating a new Source:
public Result test(){
Source<ByteString, NotUsed> delay = Source.range(0, 99999)
.map(i -> ByteString.fromString(i.toString() + "\n"))
.delay(FiniteDuration.create(200, TimeUnit.MILLISECONDS), DelayOverflowStrategy.backpressure());
HttpEntity http = new HttpEntity.Streamed(delay
, Optional.empty(), Optional.of("text/event-stream"));
return ok().sendEntity(http);
}
I have checked the data, and the item does emit individually. At first I thought Mongo might return the whole collection instead of streaming, but this was not the case. Mapping each item works, so delaying them should work as well?
I can only think this has something to do with the fromPublisher(Publisher<T>)
static method.
I'm using Play Framework 2.5, MongoDB Driver Reactive 1.3.0 and Akka 2.5.2
Any help is appreciated!