scalawebsocketakkaakka-streamplayframework-2.6

WebSocket Missing implicit Message Flow Transformer for custom data type in play 2.6


I am trying to establish a web socket connection between client and server, but an implicit MessageFlowTransformer is required for data types, the play documentation only introduces String and JsValue transformation, and I have the following data types :

sealed trait InEvent
sealed trait outEvent

  final case class AckInEevent(serial: Long) extends InEvent
  final case class AckOutEevent(serial: Long) extends outEvent
//theWebSocket action handler :

def accept = WebSocket.tryAcceptWithActor[InEvent, outEvent] { request => .....}

I got an error indication

could not find implicit value for parameter transformer: play.api.mvc.WebSocket.MessageFlowTransformer[InEvent, outEvent]

Solution

  • Try

    sealed trait InEvent
    sealed trait outEvent
    
    final case class AckInEevent(serial: Long) extends InEvent
    final case class AckOutEevent(serial: Long) extends outEvent
    
    import play.api.libs.json._
    implicit val ackInEeventFormat = Json.format[AckInEevent]
    implicit val ackOutEeventFormat = Json.format[AckOutEevent]
    implicit val inEeventFormat = Json.format[InEvent]
    implicit val outEeventFormat = Json.format[outEvent]
    
    implicit val messageFlowTransformer =
      MessageFlowTransformer.jsonMessageFlowTransformer[InEvent, outEvent]
    
    def accept = WebSocket.tryAcceptWithActor[InEvent, outEvent] { request => ???}
    

    libraryDependencies += "org.julienrf" %% "play-json-derived-codecs" % "6.0.0"

    https://github.com/coding-jam/lagioconda/blob/master/frontend/app/protocol/Protocol.scala