title: Dart How to code a simple web-socket echo app
body: <p>I've been attempting to learn enough html, css, and Dart to create my first web page and all is going well, <strong><em>except</em></strong> that I do not understand how to create a simple page and a server side web-socket server that will just echo it back.  The examples that I find tend to illustrate other Dart tools and either connect to echo server on the web or do other things that make their code not simple for a newbie.</p>
  
  <p>I've tried to simplify Seth Ladd's <a href="https://github.com/sethladd/dart-example-web-sockets-client" rel="noreferrer">example</a> "dart-example-web-sockets-client" as the 'best' example.  I can receive what is sent from the page, repackage it and <em>think</em> i'm sending it back but absolutely nothing happens on the web page.  I start the page by clicking on the URL returned when the web-server is run from inside the Dart editor.  Since the page is not, AFAIK, run in the debugger I'm hampered in diagnosing the error.</p>
  
  <p>Here is simplified code from Seth's server:</p>
  
  
  
  <pre class="lang-dart prettyprint-override"><code>void handleEchoWebSocket(WebSocket webSocket) {
  log.info('New WebSocket connection');
  
  // Listen for incoming data. We expect the data to be a JSON-encoded String.
  webSocket
    .map((string) =&gt; JSON.decode(string))
    .listen((json) {
      // The JSON object should contain a 'request' entry.
       var request = json['request'];
       switch (request) {
         case 'search':  
           var input = json['input'];
           log.info("Received request '$request' for '$input'");
           var response = {
             'response': request,
             'input': input,
        };
        webSocket.add(JSON.encode(response)); // can't detect page receiving this.
        log.info("Echoed request..$request $input"); // correct data
        break;
  
      default:
        log.warning("Invalid request: '$request'");
        }
      }, onError: (error) {
    log.warning('Bad WebSocket request');
    });
  }
  </code></pre>
  
  <p>This example took the user input using it as input to two search engines, packaged the results and returned them to the page for display creating new DOM elements on the fly.</p>
  
  <p>I just need to be pointed to a simple example that will echo what is submitted.</p>