title: Sending mass/push message from server to client in DART lang using web socket [multi sockets]
body: <p>In my app, I dedicated the footer in the client side to receive messages/notification from the admin, I thought of using the web socket [multi sockets], but the msg is sent back to one socket only, i.e. the msg is sent back to the same socket that sent it, and not sent to other sockets, hope the attached file present the issue in a simple way.</p>
  
  <p>note: All sockets are responding to socket onOpen and onClose, the bug is in the response of the onMessage!</p>
  
  <p><img src="https://i.stack.imgur.com/FKve4.png" alt="enter image description here"></p>
  
  <p>my code is:</p>
  
  
  
  <p>server.dart:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:io';
  import 'package:intl/intl.dart';  // for DateFormat
  
  class myClient {
        WebSocket _socket;
  
       myClient(WebSocket ws){
              _socket = ws;
              _socket.listen(messageHandler,
                             onError: errorHandler,
                             onDone: finishedHandler);
       }
  
       void write(String message){ _socket.add(message); }
  
      void messageHandler(String msg){  
           print('a client sent this msg $msg');
           distributeMessage(msg);
      }
  
      void errorHandler(error){
           print('one socket got error: $error');
           removeClient(this);
          _socket.close();
      }
  
      void finishedHandler() {
           print('one socket had been closed');
           distributeMessage('one socket had been closed');
           removeClient(this);
           _socket.close();
      }
  }
  
  
  List&lt;myClient&gt; clients = new List();
  
   void main() { 
        HttpServer.bind(InternetAddress.ANY_IP_V4, 8080).then((HttpServer server) {
          print("HttpServer listening...");
          server.listen((HttpRequest request) {
            if (WebSocketTransformer.isUpgradeRequest(request)){
                WebSocketTransformer.upgrade(request).then(handleWebSocket);
             }
        else {
              print("Regular ${request.method} request for: ${request.uri.path}");
              serveRequest(request);
              }
          });
       });
   }
  
  void handleWebSocket(WebSocket socket){
    print('Client connected!');
    myClient client = new myClient(socket);
    addClient(client);
  }
  
  void serveRequest(HttpRequest request){
    request.response.statusCode = HttpStatus.FORBIDDEN;
    request.response.reasonPhrase = "WebSocket connections only";
    request.response.close();
  }
  
  void distributeMessage(String msg){
     var now = new DateTime.now();
     var timeStamp = new DateFormat("[dd-MMM @h:mma]");
     String formatTime = timeStamp.format(now);   
     for (myClient c in clients)c.write('${timeStamp.format(now)}: $msg ');
   }
  
   void addClient(myClient c){
       clients.add(c);
   }
  
   void removeClient(myClient c){
        clients.remove(c);
   }
  </code></pre>
  
  <p>the client.dart file is:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:html';
  
  void main() {
         Element fonixFooter = querySelector('#fonix-footer');
  
         String server = 'ws://localhost:8080/';
         WebSocket ws = new WebSocket(server);
         ws.onOpen.listen((Event e) =&gt; fonixFooter.text='Connected to server');   // this is fine
  
         ws.onMessage.listen((MessageEvent e){        &lt;= this is not functioning!!
            print('msg is: ${e.data}');
            fonixFooter.innerHtml=e.data;
         });
  
        ws.onClose.listen((Event e) =&gt; fonixFooter.text='Connection to server lost...');   // this is fine
  
     }
  </code></pre>
  
  <p>the admin.dart is:</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'dart:html';
  
   void main() {
        TextInputElement input = querySelector('#input');
        ParagraphElement output = querySelector('#output');
  
        String server = 'ws://localhost:8080/';
        WebSocket ws = new WebSocket(server);
        ws.onOpen.listen((Event e) {
        outputMessage(output, 'Connected to server');
    });
  
    ws.onMessage.listen((MessageEvent e){
        outputMessage(output, e.data);
    }); 
  
    ws.onClose.listen((Event e) {
       outputMessage(output, 'Connection to server lost...');
    });
  
    input.onChange.listen((Event e){
       ws.send(input.value.trim());
       input.value = "";
     });
  }
  
   void outputMessage(Element e, String message){
        print(message);
        e.appendText(message);
        e.appendHtml('&lt;br/&gt;');
        e.scrollTop = e.scrollHeight;
   }
  </code></pre>
  
  <p>based on @luizmineo feedback the he tested the server side, I noticed my code is working if and only if each socket sent some data, the socket that is not sending data, not receiving anything, but still do not understand why! and how to fix it! </p>
  
  <p>Any thought, where I made the mistake!! any recommendation about better approach is appreciated.</p>