Search code examples
scalaplayframeworkakka

Getting 'unable to resolve overloaded method actorRef' error when building websockets in Play + Scala


I'm trying to follow the instructions on the Play website here to set up a basic websocket. However, when trying to use the following code I'm getting an error:

Cannot resolve overloaded method 'actorRef'

Here is the code:

package controllers

import play.api.mvc._

import javax.inject._
import akka.actor._
import akka.stream.Materializer
import play.libs.streams.ActorFlow


@Singleton
class HomeController @Inject()(cc: ControllerComponents)(implicit system: ActorSystem, mat: Materializer) extends AbstractController(cc) {

  def ws = WebSocket.accept[String, String] { request =>
    ActorFlow.actorRef { out =>
      MyWebSocketActor.props(out)
    }
  }
}

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

class MyWebSocketActor(out: ActorRef) extends Actor {
  def receive = {
    case msg: String =>
      out ! ("I received your message: " + msg)
  }
}

What is the right way to set up a controller to accept a websocket connection?


Solution

  • Okay so it turns out I was incorrectly using the Java API rather than the Scala one when doing an import. I changed the following:

    import play.libs.streams.ActorFlow
    

    to

    import play.api.libs.streams.ActorFlow
    

    This resolved the issue!