A simple implementation of (most of) the Phoenix Channels protocol with akka HTTP for WebSocket communication and circe for JSON
akka HTTP Phoenix is available for Scala 2.11
and 2.12
libraryDependencies += "io.taig" %% "akka-http-phoenix" % "1.0.0"
import akka.actor.ActorSystem
import akka.http.scaladsl.model.ws.WebSocketRequest
import akka.stream.ActorMaterializer
import io.circe.syntax._
import io.taig.akka.http.phoenix._
import scala.concurrent._
import scala.concurrent.duration._
import scala.language.postfixOps
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
val request = WebSocketRequest( "ws://localhost:4000/socket/websocket" )
val topic = Topic( "echo", "foobar" )
import system.dispatcher
val future = for {
// Open socket connection to the Phoenix server
phoenix ← Phoenix( request )
// Join a channel
Right( channel ) ← phoenix.join( topic )
// Send a message and wait for response
Result.Success( response ) ← channel.send( Event( "echo" ), "foobar".asJson )
// Shutdown socket connection from the client side
_ = phoenix.close()
} yield response
scala> try {
| Await.result( future, 5 seconds )
| } finally {
| Await.result( system.terminate(), 5 seconds )
| }
res5: io.taig.akka.http.phoenix.message.Response =
Response(Topic(echo:foobar),Event(phx_reply),Some(Payload(Status(ok),{
"payload" : "foobar"
})),Ref(1))
Code executed by the unit tests or documentation generation via tut requires the phoenix_echo app to be running in the background. Use the provided Dockerfile
to create an image with pre-installed requirements.