Search code examples
scalawebsocketlagom

Secure websockets with Lagom


With the lightbend Lagom framework I'm trying to connect to the websocket api of Binance.

However, I'm keep getting the following error on connecting:

400 The plain HTTP request was sent to HTTPS port

Should it be possible from Lagom to connect to a secure websocket service? So with WebSocketClient? I have the following code:

trait BinanceStreamingService extends Service {
    def depthStream(symbol: String): ServiceCall[NotUsed, Source[DepthEvent, NotUsed]]

    override final def descriptor = {
        import Service._
        import me.koopal.crypto.api.BinanceModelsMarshallers._

        named("depth-stream")
            .withCalls(
                restCall(GET, "/ws/:symbol@deth", depthStream _)
            )
     }
} 

private val binanceStreamApplication = new LagomClientApplication("binance-ws") with StaticServiceLocatorComponents with AhcWSComponents {
    override def staticServiceUri = URI.create("wss://stream.binance.com:9443")
}

override def stream = ServiceCall { _ =>
    binanceStreamClient.depthStream("bnbbtc")
        .invoke()
        .map { s =>
            s.runForeach(e => println(e))
        }.onComplete {
            case Success(x) => println("success", x)
            case Failure(ex) => println("failure", ex)
        }

        Future.successful("test")
}

A ruuning code example can be found here: https://github.com/stijnkoopal/lagom-binance-websockets


Solution

  • Lagom's WebSocket client does not yet support TLS. There is an open issue to reimplement the client using Akka HTTP, which will enable TLS support: https://github.com/lagom/lagom/issues/895

    In the meantime, the best approach is to implement your client using Akka HTTP Client-Side WebSocket Support or another WebSocket client library that supports secure connections.