Is it abuse or somehow dangerous to use akka-http like this?
On server
def source(consumerOffset: UUID) =
readJournal.eventsByTag(“MyTag", consumerOffset).map(_.asJson)
pathPrefix("stream" / Segment.map(UUID.fromString)) { offset =>
pathEndOrSingleSlash {
get {
complete {
HttpResponse(
StatusCodes.OK,
entity = HttpEntity(ContentTypes.`application/json`, source(offset))
)
}
}
}
}
Then on client side
Source.single(HttpRequest("http://localhost:9000/stream"))
.mapAsync(1) { r =>
Http().singleRequest(r).map { res =>
res.entity.dataBytes.map(_.parse[Event])
}
}
.flatMapConcat(identity).mapAsync(processEvent)
UPD:
- Is it guaranteed that chunks I send will be the same on client side.
- Is it ok to have response with possibly endless number of chunks?
- What is the right Content-Type for this kind of response?
UPD 2:
Akka 2.4.9 added ability to respond with streams. And basically does exactly the same, providing some syntax sugar. See the docs.