user3365
user3365

Reputation: 31

Scala / Play2 - WebSocket Enumerator and Actor

I have a web page to submit long running jobs. Submitting a job will provide a link to a detailed job view, containing log messages that occur when running the job. I wan't the messages to be displayed when they are available without having to reload the page.

Therefor I create a WebSocket connection using JavaScript and a corresponding route. My idea was to use actors (akka) to pass the messages from the server side to the client side.

How can I achieve this using scala 2.11 and play 2.3.4?

best regards!

Upvotes: 0

Views: 228

Answers (1)

Andreas Neumann
Andreas Neumann

Reputation: 10904

Here you get a detailed description: https://www.playframework.com/documentation/2.3.x/ScalaWebSockets

Here is a rough description based on your requirements

First you create an action using WebSocket.acceptWithActor

case class MyMessage(s: String)

def socket = WebSocket.acceptWithActor[MyMessage, JsValue] { request => out =>
  MyWebSocketActor.props(out)
}

object MyWebSocketActor {
   def props(out: ActorRef) = Props(new MyWebSocketActor(out))
}

class MyWebSocketActor(out: ActorRef) extends Actor {
  def receive = {
    case msg: MyMessage =>
     out ! Json.toJson(msg)
  }

Upvotes: 1

Related Questions