Use WebSocket connection using Scala and Play

I am looking for a way to use websocket in Scala.

The web application is created in the playback application. Here's the code in


def socket = WebSocket.acceptWithActor[String, JsValue] { request => out =>


<strong> routes

# Sockets
GET     /socket                          controllers.MyController.socket


Connecting to a socket using Javascript works great. Now I want to connect to the socket using http-Client. I am currently using PlayHttpClient

Sbt dependecy client

"" %% "play-ws" % "2.3.4"


Code for using Websocket

I found several methods on the client that could do the trick, but I am lost on which one to use and how to use them:

def get[A](consumer: (WSResponseHeaders) ⇒ Iteratee[Array[Byte], A])(implicit ec: ExecutionContext): Future[Iteratee[Array[Byte], A]]


looks like it might do the trick. But how to realize the consumer?

def getStream(): Future[(WSResponseHeaders, Enumerator[Array[Byte]])]

// Thats how I used it
  def alertStream = client.
    withHeaders("Accept" -> "application/json"). { case (headers, enumerator) =>

      val loggingIteratee = Iteratee.foreach[Array[Byte]] { chunk =>
        val chunkString = new String(chunk, "UTF-8")

     enumerator.apply( loggingIteratee )



I tried this, but I keep getting 400-Excetchion in ResponseHeaders. Changing the protocol to ws: // in the url results in this exception:

> val con = client.url("ws://localhost:9000/socket").get WebSocket method must be a GET
at    com.ning.http.client.providers.netty.NettyAsyncHttpProvider.doConnect( 



source to share

All Articles