title: Playframework websocket live streaming from server to browser
body: <p>I am trying to continuously stream data from webserver to browser using playframework. I created a sample code based from example <a href="http://www.playframework.org/documentation/2.0.3/JavaWebSockets" rel="nofollow">documentation</a></p>
  
  <p>Here is the snippet of code I have</p>
  
  <pre><code>public static WebSocket&lt;String&gt; sockHandler() {
      return new WebSocket&lt;String&gt;() {
          // called when the websocket is established
          public void onReady(WebSocket.In&lt;String&gt; in, final WebSocket.Out&lt;String&gt; out) {
              // register a callback for processing instream events
              in.onMessage(new F.Callback&lt;String&gt;() {
                  public void invoke(String event) {
                      Logger.info(event);
                  }
              });
  
  
              int i = 0;
              while(i&lt;20){
                  Logger.info("Sending message ");
                  try{
                      Thread.sleep(1000);
                  }catch (Exception e){
                     Logger.error("Exception " ,e);
                  }
  
                  out.write("Sending message " + i++ + "\n");
  
              }
          }
      };
  }
  </code></pre>
  
  <p>On the browser, the messages written to out is not received immediately after each iteration. Messages are received in browsers all at once after while loop completes. </p>
  
  <p>Is there a way to flush out stream after each iteration..? Or is there a better example/documentation to stream live data from websocket server to browser..?</p>
  
  <p>Thanks,
  Raja.</p>