0

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:

  1. Is it guaranteed that chunks I send will be the same on client side.
  2. Is it ok to have response with possibly endless number of chunks?
  3. 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.

Denis Mikhaylov
  • 2,035
  • 21
  • 24

1 Answers1

1

Answering your questions in order:

  1. Yes, the client will receive BytesString representations of your objects encoded in json.
  2. Yes, it is fine to have a stream source on the server side which never terminates.
  3. The content you've specified in the question (application/json) is correct.
Ramón J Romero y Vigil
  • 17,373
  • 7
  • 77
  • 125