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
        new MqttCallback() {
        override def connectionLost(cause: Throwable): Unit = {

        override def messageArrived(topic: String, message: MqttMessage): Unit = {

        override def deliveryComplete(token: IMqttDeliveryToken): Unit = {

val websocketRoute = get {

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