Akka-Http Web Socket how to send multiple response to a client

Hi, I want to design a web-socket to get a message from the user and keeping the connection, send multiple message to the server (for example the server side will establish a connection to the mMQTT message broker and consume messages and send them to the client continuously).
Using the Akka-http I developed the following:

def echoService() Flow[Message, Message, _] = Flow[Message].map{
  case TextMessage.Strict(txt) =>
    val temp = password.split("/")
    if (temp.nonEmpty && temp.length < 4) {
      val persistence = new MemoryPersistence
      val client = new MqttClient(url, MqttClient.generateClientId(), persistence)
      val MQtt_Option: MqttConnectOptions = new MqttConnectOptions
      MQtt_Option.setCleanSession(true)
      MQtt_Option.setUserName(userName)
      MQtt_Option.setPassword(password.toCharArray)
      client.setCallback(
        new MqttCallback() {
        override def connectionLost(cause: Throwable): Unit = {

        }
        @throws[Exception]
        override def messageArrived(topic: String, message: MqttMessage): Unit = {
          TextMessage(message.getPayload.toString)
        }

        override def deliveryComplete(token: IMqttDeliveryToken): Unit = {
        }
      })
      client.connect(MQtt_Option)
      client.subscribe(txt)
    }
    TextMessage("")
}

val websocketRoute = get {
      handleWebSocketMessages(echoService())
    }

But it just receive a message and send just one message and then disconnect! I found out that is behavior of the Flow object but I don’t know how should I change mu code to receive just one message from the client and send client continuously the message broker messages!

Any help or suggestion would be appreciated!

This actually isn’t the best place for this question – we’re mostly focused on the language per se here. But there is a similar forum specifically for Akka, which is more likely to have folks who can help: I’d recommend asking this over there…

1 Like