Ciaran0
Ciaran0

Reputation: 428

Play framework How to use akka streams output to a websocket

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

Upvotes: 0

Views: 1210

Answers (2)

lutzh
lutzh

Reputation: 4965

"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.

Upvotes: 0

Ahmed Farghal
Ahmed Farghal

Reputation: 1314

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")
  }

Upvotes: 1

Related Questions