Raja
Raja

Reputation: 1327

Playframework websocket live streaming from server to browser

I am trying to continuously stream data from webserver to browser using playframework. I created a sample code based from example documentation

Here is the snippet of code I have

public static WebSocket<String> sockHandler() {
    return new WebSocket<String>() {
        // called when the websocket is established
        public void onReady(WebSocket.In<String> in, final WebSocket.Out<String> out) {
            // register a callback for processing instream events
            in.onMessage(new F.Callback<String>() {
                public void invoke(String event) {
                    Logger.info(event);
                }
            });


            int i = 0;
            while(i<20){
                Logger.info("Sending message ");
                try{
                    Thread.sleep(1000);
                }catch (Exception e){
                   Logger.error("Exception " ,e);
                }

                out.write("Sending message " + i++ + "\n");

            }
        }
    };
}

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.

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..?

Thanks, Raja.

Upvotes: 2

Views: 1418

Answers (1)

ndeverge
ndeverge

Reputation: 21564

I strongly advise you to take a look at this sample.

It uses Websockets and Akka actors. The Actor system is not very difficult to understand, the Akka documentation is a good place to start !

Upvotes: 1

Related Questions