Streaming JSON objects using akka http chunked response
<p>Is it abuse or somehow dangerous to use akka-http like this?</p>

<p>On server</p>

<pre><code>def source(consumerOffset: UUID) = 
  readJournal.eventsByTag(“MyTag", consumerOffset).map(_.asJson)

pathPrefix("stream" / Segment.map(UUID.fromString)) { offset =&gt;
  pathEndOrSingleSlash {
    get {
      complete {
        HttpResponse(
          StatusCodes.OK,
          entity = HttpEntity(ContentTypes.`application/json`, source(offset))
        )
      }
    }
  }
}
</code></pre>

<p>Then on client side</p>

<pre><code>Source.single(HttpRequest("http://localhost:9000/stream"))
  .mapAsync(1) { r =&gt;
    Http().singleRequest(r).map { res =&gt;
      res.entity.dataBytes.map(_.parse[Event])
    }
  }
  .flatMapConcat(identity).mapAsync(processEvent)
</code></pre>

<p><strong>UPD:</strong></p>

<ol>
<li>Is it guaranteed that chunks I send will be the same on client side. </li>
<li>Is it ok to have response with possibly endless number of chunks?</li>
<li>What is the right Content-Type for this kind of response?</li>
</ol>

<p><strong>UPD 2:</strong></p>

<p>Akka 2.4.9 added ability to respond with streams. And basically does exactly the same, providing some syntax sugar.
See the <a href="http://doc.akka.io/docs/akka/2.4/scala/http/routing-dsl/source-streaming-support.html" rel="nofollow">docs</a>.</p>