akka-stream

BroadcastHub filtering based on “resource” the connected client is working on?

早过忘川 提交于 2019-12-04 06:03:45
I am writing a pure websocket web application, meaning that prior to the websocket upgrade there is no user/client step, more specifically: Authentication request goes over websockets as does the rest of the communication There is/are: Exactly ONE websocket endpoint on /api/ws Multiple clients connected to that endpoint Multiple projects for multiple clients Now, not each client has access to each project - the access control for that is implemented on the server side (ofc) and has nothing to do with websockets per se. My problem is, that I want to allow collaboration, meaning that N clients

akka-stream - How to treat the last element of a stream differently in a Flow/Graph

孤街浪徒 提交于 2019-12-04 05:06:57
I'm trying to implement an Akka Streams Flow that will convert a stream of JSON objects to a stream of a single array of JSON objects. I can use Concat to add an "[" before and "]" after, as well as Zip to insert commas in between elements, but I can't figure out how to not insert the final comma. The code I have so far is: trait JsonStreamSupport { protected def toJsonArrayString[T : Writes] = Flow[T].map(Json.toJson(_)).map(_.toString()).via(jsonArrayWrapper) private[this] val jsonArrayWrapper: Flow[String, String, NotUsed] = Flow.fromGraph(GraphDSL.create() { implicit b => import GraphDSL

Handle Akka stream's first element specially

风格不统一 提交于 2019-12-04 03:54:31
Is there an idiomatic way of handling Akka stream's Source first element in a special way? What I have now is: var firstHandled = false source.map { elem => if(!firstHandled) { //handle specially firstHandled = true } else { //handle normally } } Thanks While I would generally go with Ramon's answer, you could also use prefixAndTail , with a prefix of 1, together with flatMapConcat to achieve something similar: val src = Source(List(1, 2, 3, 4, 5)) val fst = Flow[Int].map(i => s"First: $i") val rst = Flow[Int].map(i => s"Rest: $i") val together = src.prefixAndTail(1).flatMapConcat { case (head

How do I supply an implicit value for an akka.stream.Materializer when sending a FakeRequest?

岁酱吖の 提交于 2019-12-04 01:58:20
I'm trying to make sense of the error(s) I'm seeing below, and to learn how to fix it. could not find implicit value for parameter materializer: akka.Stream.Materializer val fut: Future[Result] = action.apply(fakeRequest).run ^ not enough arguments for method run (implicit materializer: akka.stream.Materializer)scala.concurrent.Future[play.api.mvc.Result]. Unspecified value parameter materializer. val fut: Future[Result] = action.apply(fakeRequest).run ^ Here is the test code that produced the error(s): package com.foo.test import com.foo.{Api, BoundingBox} import org.scalatest.{FlatSpec,

How to log flow rate in Akka Stream?

岁酱吖の 提交于 2019-12-03 21:54:56
I have an Akka Stream application with a single flow/graph. I want to measure the flow rate at the source and log it every 5 seconds, like 'received 3 messages in the last 5 seconds'. I tried with, someOtherFlow .groupedWithin(Integer.MAX_VALUE, 5 seconds) .runForeach(seq => log.debug(s"received ${seq.length} messages in the last 5 seconds") ) but it only outputs when there are messages, no empty list when there are 0 messages. I want the 0's as well. Is this possible? You could try something like src .conflateWithSeed(_ ⇒ 1){ case (acc, _) ⇒ acc + 1 } .zip(Source.tick(5.seconds, 5.seconds,

How do I dynamically add Source to existing Graph?

橙三吉。 提交于 2019-12-03 19:42:06
问题 What can be alternative to dynamically changing running graph ? Here is my situation. I have graph that ingests articles into DB. Articles come from 3 plugins in different format. Thus I have several flows val converterFlow1: Flow[ImpArticle, Article, NotUsed] val converterFlow2: Flow[NewsArticle, Article, NotUsed] val sinkDB: Sink[Article, Future[Done]] // These are being created every time I poll plugins val sourceContentProvider : Source[ImpArticle, NotUsed] val sourceNews : Source

Consume TCP stream and redirect it to another Sink (with Akka Streams)

删除回忆录丶 提交于 2019-12-03 16:36:09
I try to redirect/forward a TCP stream to another Sink with Akka 2.4.3. The program should open a server socket, listen for incoming connections and then consume the tcp stream. Our sender does not expect/accept replies from us so we never send back anything - we just consume the stream. After framing the tcp stream we need to transform the bytes into something more useful and send it to the Sink. I tried the following so far but i struggle especially with the part how to not sending tcp packets back to the sender and to properly connect the Sink. import scala.util.Failure import scala.util

Streaming file from server to client using Akka

徘徊边缘 提交于 2019-12-03 15:30:54
Basically I want to allow a user to download a csv file from the server. Assume the CSV file already exists on the server. A API endpoint is exposed via GET /export. How do I stream the file from Akka HTTP server to client? This is what I have so far... Service: def export(): Future[IOResult] = { FileIO.fromPath(Paths.get("file.csv")) .to(Sink.ignore) .run() } Route: pathPrefix("export") { pathEndOrSingleSlash { get { complete(HttpEntity(ContentTypes.`text/csv`, export()) } } } The Akka-Stream API allow you to create an entity directly out of a Source[ByteString, _] , so you can do something

Get whole HttpResponse body as a String with Akka-Streams HTTP

可紊 提交于 2019-12-03 13:56:56
I'm trying to understand how to use the new akka.http library. I would like to send an http request to a server and read the whole response body as a single String in order to produce a Source[String,?] . Here is the best solution I was able to produce so far: def get( modelID: String, pool: Flow[(HttpRequest,Int),(Try[HttpResponse],Int),Http.HostConnectionPool] ): Source[String,Unit] = { val uri = reactionsURL(modelID) val req = HttpRequest(uri = uri) Source.single( (req,0) ) .via( pool ) .map { case (Success(resp),_) => resp.entity.dataBytes.map( _.decodeString("utf-8") ) }.flatten

Akka Flow hangs when making http requests via connection pool

*爱你&永不变心* 提交于 2019-12-03 13:33:59
I'm using Akka 2.4.4 and trying to move from Apache HttpAsyncClient (unsuccessfully). Below is simplified version of code that I use in my project. The problem is that it hangs if I send more than 1-3 requests to the flow. So far after 6 hours of debugging I couldn't even locate the problem. I don't see exceptions, error logs, events in Decider . NOTHING :) I tried reducing connection-timeout setting to 1s thinking that maybe it's waiting for response from the server but it didn't help. What am I doing wrong ? import akka.actor.ActorSystem import akka.http.scaladsl.Http import akka.http