title: How to listen websocket server close events using akka-http websocket client
body: <p>I have websocket client connected to akka-http websocket server , how can i listen to connection close events happened on server( i.e server shutdown/ server closed websocket connection) ? </p>
  
  <pre><code>object Client extends App {
  
  
    implicit val actorSystem = ActorSystem("akka-system")
    implicit val flowMaterializer = ActorMaterializer()
  
    val config = actorSystem.settings.config
    val interface = config.getString("app.interface")
  
    val port = config.getInt("app.port")
  
  
    // print each incoming strict text message
    val printSink: Sink[Message, Future[Done]] =
      Sink.foreach {
        case message: TextMessage.Strict =&gt;
          println(message.text)
  
        case _ =&gt; {
          sourceQueue.map(q =&gt; {
            println(s"offering message on client")
            q.offer(TextMessage("received unknown"))
          })
          println(s"received unknown message format")
        }
      }
  
    val (source, sourceQueue) = {
      val p = Promise[SourceQueue[Message]]
      val s = Source.queue[Message](Int.MaxValue, OverflowStrategy.backpressure).mapMaterializedValue(m =&gt; {
        p.trySuccess(m)
        m
      })
        .keepAlive(FiniteDuration(1, TimeUnit.SECONDS), () =&gt; TextMessage.Strict("Heart Beat"))
      (s, p.future)
    }
  
    val flow =
      Flow.fromSinkAndSourceMat(printSink, source)(Keep.right)
  
  
    val (upgradeResponse, sourceClose) =
      Http().singleWebSocketRequest(WebSocketRequest("ws://localhost:8080/ws-echo"), flow)
  
    val connected = upgradeResponse.map { upgrade =&gt;
      // just like a regular http request we can get 404 NotFound,
      // with a response body, that will be available from upgrade.response
      if (upgrade.response.status == StatusCodes.SwitchingProtocols || upgrade.response.status == StatusCodes.SwitchingProtocols) {
        Done
      } else {
        throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
      }
    }
  
  
    connected.onComplete(println)
  
  }
  </code></pre>