Play framework How to use akka streams output to a websocket
Asked Answered
T

2

0

Whats the easiest way to have the output of a flow be sent to a web socket in Playframework with Scala.

I want the WebSocket to act as the sink of the stream. For example the source generate a random number and then go to the sink (Websocket) so it is pushed to client.

Thanks

Tarr answered 17/12, 2015 at 17:54 Comment(0)
G
1

If you are talking about the new Akka streams integration in Play 2.5.x (still M2), then I think this should do the job as a simple valid flow that represents an echo server (with a touch).

  def socket = WebSocket.accept[String, String] {
    request =>
      Flow[String]
        .map(_ + " Back")
  }
Gigue answered 24/1, 2016 at 19:52 Comment(0)
E
0

"The easiest" is somewhat subjective, but you might consider Play's experimental reactive streams support as described here.

Include the Reactive Streams integration library into your project.

libraryDependencies += "com.typesafe.play" %% "play-streams-experimental" % "2.4.4"

All access to the module is through the Streams object.

Here is an example that adapts a Future into a single-element Publisher.

val fut: Future[Int] = Future { ... }
val pubr: Publisher[Int] = Streams.futureToPublisher(fut)

See the Streams object’s API documentation for more information.

Eurydice answered 19/12, 2015 at 20:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.