Search code examples
javascalawebsocketakkaakka-http

Best way to keep WebSocket connection alive in Akka HTTP?


If there is no message sent or received, the connection is closed after one minute.

I don't want to continuously send a text message to keep the connection open like this:

keepAlive(maxIdle = 10.seconds, () => TextMessage.Strict("Keep-alive message")

Is there anything in Akka HTTP that provides the natural sending of ping/pong messages? What do I need to do?


Solution

  • If you don't want to manually use the keepAlive combinator, then you can use Akka HTTP's automatic keep-alive ping support, which is enabled via settings in your application.conf.

    For the client side:

    akka.http.client.websocket.periodic-keep-alive-max-idle = 10 seconds
    

    For the server side:

    akka.http.server.websocket.periodic-keep-alive-max-idle = 10 seconds
    

    More information is in the linked documentation.