Andriy has more than 12 years of software development experience as Programmer/Software Developer/Senior Software Developer/Team Lead/Consultant, working extensively with Microsoft .NET, Sun JEE and Adobe Flex platforms using Java, C#, C++, Groovy, Scala, Ruby, Action Script, Grails, MySQL, PostreSQL, MongoDB, and more. Andriy is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

Real-time Charts With Play Framework and Scala: Extreme Productivity on JVM for Web

  • submit to reddit

Being a hardcore back-end developer, whenever I am thinking about building web application with some UI on JVM platform, I feel scared. And there are reasons for that: having experience with JSFLiferayGrails, ... I don't want to go this road anymore. But if a need comes, is there a choice, really? I found one which I think is awesome: Play Framework.

Built on top of JVM, Play Framework allows to create web applications using Java or Scala with literally no efforts. The valuable and distinguishing differences it provides: static compilation (even for page templates), easy to start with, and concise (more about it here).

To demonstrate how amazing Play Framework is, I would like to share my experience with developing simple web application. Let's assume we have couple of hosts and we would like to watch CPU usage on each one in real-time (on a chart). When one hears "real-time", it may mean different things but in context of our application it means: using WebSockets to push data from server to client. Though Play Framework supports pure Java API, I will use some Scala instead as it makes code very compact and clear.

Let's get started! After downloading Play Framework (the latest version on the moment of writing was 2.1.1), let's create our app by typing

play new play-websockets-example

and selecting Scala as a primary language. No wonders here: it's a pretty standard way nowadays, right?

Having our application ready, next step would be to create some starting web page. Play Framework uses own type safe template engine based on Scala, it has a couple of extremely simple rules and is very easy to get started with. Here is an example of views/dashboard.scala.html:

@(title: String, hosts: List[Host])

<!DOCTYPE html>
    <link rel="stylesheet" media="screen" href=""stylesheets/main.css")">
    <link rel="shortcut icon" type="image/png" href=""images/favicon.png")">
    <script src=""javascripts/jquery-1.9.0.min.js")" type="text/javascript">
    <script src=""javascripts/highcharts.js")" type="text/javascript">
    <div id="hosts">
      <ul class="hosts"> { host =>
          <a href="#" onclick="javascript:show( '' )"><b></b></a>
    <div id="content">

<script type="text/javascript">
function show( hostid ) {
  $("#content").load( "/host/" + hostid,
    function( response, status, xhr ) {
      if (status == "error") {
        $("#content").html( "Sorry but there was an error:" + xhr.status + " " + xhr.statusText);

Aside from coupe of interesting constructs (which are very well described here), it looks pretty like regular HTML with a bit of JavaScript. The result of this web page is a simple list of hosts in the browser. Whenever user clicks on a particular host, another view will be fetched from the server (using old buddy AJAX) and displayed on right side from the host. Here is the second (and the last) template, views/host.scala.html:

@(host: Host)( implicit request: RequestHeader )

<div id="content">
  <div id="chart">
  <script type="text/javascript">
    var charts = []   
    charts[ '' ] = new Highcharts.Chart({                 
      chart: {
        renderTo: 'chart',
        defaultSeriesType: 'spline'            
      xAxis: {
        type: 'datetime'
      series: [{
        name: "CPU",
        data: []

<script type="text/javascript">
var socket = new WebSocket("@routes.Application.stats( ).webSocketURL()")
socket.onmessage = function( event ) { 
  var datapoint = jQuery.parseJSON( );
  var chart = charts[ '' ]
  chart.series[ 0 ].addPoint({
    x: datapoint.cpu.timestamp,
    y: datapoint.cpu.load
  }, true, chart.series[ 0 ].data.length >= 50 );

It's looks rather as a fragment, not a complete HTML page, which has only a chart and opens the WebSocketsconnection with a listener. With an enormous help of Highcharts and jQuery, JavaScript programming hasn't ever been so easy for back-end developers as it's now. At this moment, the UI part is completely done. Let's move on to back-end side.

Firstly, let's define the routing table which includes only three URLs and by default is located at conf/routes:

GET     /                           controllers.Application.index
GET     /host/:id          id: String )
GET     /stats/:id                  controllers.Application.stats( id: String )

Having views and routes defined, it's time to fill up the last and most interesting part, the controllers which glue all parts together (actually, only one controller, controllers/Application.scala). Here is a snippet which maps index action to the view templated by views/dashboard.scala.html, it's as easy as that:

def index = Action {
  Ok( views.html.dashboard( "Dashboard", Hosts.hosts() ) )

The interpretation of this action may sound like that: return successful response code and render templateviews/dashboard.scala.html with two parameters, title and hosts, as response body. The action to handle/host/:id looks much the same:

def host( id: String ) = Action { implicit request =>
  Hosts.hosts.find( == id ) match {
    case Some( host ) => Ok( host ) )
    case None => NoContent

And here is a Hosts object defined in models/Hosts.scala. For simplicity, the list of hosts is hard-coded:

package models

case class Host( id: String, name: String )

object Hosts {  
  def hosts(): List[ Host ] = {
    return List( new Host( "h1", "Host 1" ), new Host( "h2", "Host 2" ) )

The boring part is over, let's move on to the last but not least implementation: server push of host's CPU statistics using WebSockets. As you can see, the /stats/:id URL is already mapped to controller action so let's take a look on its implementation:

def stats( id: String ) = WebSocket.async[JsValue] { request =>
  Hosts.hosts.find( == id ) match {
    case Some( host ) => Statistics.attach( host )
    case None => {
      val enumerator = Enumerator
        .generateM[JsValue]( Promise.timeout( None, 1.second ) )
        .andThen( Enumerator.eof )
      Promise.pure( ( Iteratee.ignore[JsValue], enumerator ) )

Not too much code here but in case you are curious about WebSockets in Play Framework please follow this link. This couple of lines may look a bit weird at first but once you read the documentation and understand basic design principles behind Play Framework, it will look much more familiar and friendly. The Statistics object is the one who does the real job, let's take a look on the code:

package models

import scala.concurrent.Future
import scala.concurrent.duration.DurationInt

import akka.pattern.ask
import akka.util.Timeout
import play.api.Play.current
import play.api.libs.concurrent.Akka
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.iteratee.Enumerator
import play.api.libs.iteratee.Iteratee
import play.api.libs.json.JsValue

case class Refresh()
case class Connect( host: Host )
case class Connected( enumerator: Enumerator[ JsValue ] )

object Statistics {
  implicit val timeout = Timeout( 5 second )
  var actors: Map[ String, ActorRef ] = Map()
  def actor( id: String ) = actors.synchronized {
    actors.find( _._1 == id ).map( _._2 ) match {
      case Some( actor ) => actor      
      case None => {
        val actor = Akka.system.actorOf( Props( new StatisticsActor(id) ), name = s"host-$id" )   
        Akka.system.scheduler.schedule( 0.seconds, 3.second, actor, Refresh )
        actors += ( id -> actor )
  def attach( host: Host ): Future[ ( Iteratee[ JsValue, _ ], Enumerator[ JsValue ] ) ] = {
    ( actor( ) ? Connect( host ) ).map {      
      case Connected( enumerator ) => ( Iteratee.ignore[JsValue], enumerator )

As always, thanks to Scala conciseness, not too much code but a lot of things are going on. As we may have hundreds of hosts, it would be reasonable to dedicate to each host own worker (not a thread) or, more precisely, own actor. For that, we will use another amazing library called Akka. The code snippet above just creates an actor for the host or uses existing one from the registry of the already created actors. Please note that the implementation is quite simplified and leaves off important details. The thoughts in right direction would be using supervisors and other advanced concepts instead of synchronized block. Also worth mentioning that we would like to make our actor a scheduled task: we ask actor system to send the actor a message Refresh every 3 seconds. That means that the charts will be updated with new values every three seconds as well.

So, when actor for a host is created, we send him a message Connect notifying that a new connection is being established. When response message Connected is received, we return from the method and at this point connection over WebSockets is about to be established. Please note that we intentionally ignore any input from the client by using Iteratee.ignore[JsValue].

And here is the StatisticsActor implementation:

package models

import java.util.Date

import scala.util.Random

import play.api.libs.iteratee.Concurrent
import play.api.libs.json.JsNumber
import play.api.libs.json.JsObject
import play.api.libs.json.JsString
import play.api.libs.json.JsValue

class StatisticsActor( hostid: String ) extends Actor {
  val ( enumerator, channel ) = Concurrent.broadcast[JsValue]
  def receive = {
    case Connect( host ) => sender ! Connected( enumerator )       
    case Refresh => broadcast( new Date().getTime(), hostid )
  def broadcast( timestamp: Long, id: String ) {
    val msg = JsObject( 
        "id" -> JsString( id ),
        "cpu" -> JsObject( 
            ( "timestamp" -> JsNumber( timestamp ) ), 
            ( "load" -> JsNumber( Random.nextInt( 100 ) ) ) 
    channel.push( msg )

The CPU statistics is randomly generated and the actor just broadcasts it every 3 seconds as simple JSON object. On the client side, the JavaScript code parses this JSON and updates the chart. Here is how it looks like for two hosts, Host 1 and Host 2 in Mozilla Firefox:

To finish up, I am personally very excited with what I've done so far with Play Framework. It took just couple of hours to get started and another couple of hours to make things work as expected. The errors reporting and feedback cycle from running application are absolutely terrific, thanks a lot to Play Framework guys and the community around it. There are still a lot of things to learn for me but it worth doing it.

Please find the complete source code on GitHub.

Published at DZone with permission of Andriy Redko, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Bharadwaj Narasimha replied on Mon, 2013/10/21 - 10:07am

Thanks Andriy! I was planning to write something like this and thanks to you I now have a headstart.

Lara Roi replied on Thu, 2013/10/31 - 8:33am

Which template you use here. can I find the template in activator?


Andriy Redko replied on Thu, 2013/10/31 - 6:38pm in response to: Lara Roi

Hi Lara,

Not sure Typesafe has template for that yet, I haven't use one.

Thank you.

Lara Roi replied on Wed, 2014/01/15 - 10:33am

Can you help me how to manipulate this JSon Object. It generates now the random data JsNumber(Random.nextInt(100). I want set a value as if any randomly generated value  goes under 80. It will stop to generate the data. And will display those 20 randomly generated data in bar form. or may be if value goes under 80, it ll make an alarm to ask for handle. Will I need to do it from client side or server side?

Will be helpful if you reply about this.


Andriy Redko replied on Wed, 2014/01/15 - 5:46pm in response to: Lara Roi

Hi Lara,

Sure, my pleasure to help you. If I understand correctly, you would like to alarm somehow that the value is going over 80. If that's the case, you may take a simplest approach: just use JavaScript (client-side) to visualize this fact, or, a bit complicated one, trigger an event on server-side (f.e., e-mail, tweet, ...), or do both.

Please let me know if it helps.


Best Regards,

    Andriy Redko

Lara Roi replied on Thu, 2014/01/16 - 6:52am

Hi, How can I store /take those randomly generated data in client side?


Andriy Redko replied on Thu, 2014/01/16 - 5:33pm in response to: Lara Roi

Hi Lara,

It's quite easy. In the post server sends JSON to the client like that:


    "cpu": {

        "timestamp": "...",

        "load": "..."


To handle this JSON on client side, we do that:

socket.onmessage = function( event ) { 

  // contains the same JSON as the example above

  var datapoint = jQuery.parseJSON( ); 

 //  datapoint.cpu.timestamp - accesses cpu.timestamp

 //  datapoint.cpu.load - accesses cpu.load

 // datapoint.<my_generated_value> - accesses <my_generated_value> you may send off



Hope it helps! Thanks.

Lara Roi replied on Fri, 2014/01/24 - 8:21am

Thanks. Can I get your mail id . I have something to discuss.


Andriy Redko replied on Fri, 2014/01/24 - 4:48pm

Sure, please drop me a message to drreta at Thanks!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.