alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Comet.scala)

This example Play Framework source code file (Comet.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

a, api, comet, cometmessage, enumeratee, html, iterate, iteratee, lib, library, play, play framework, string

The Comet.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs

import org.apache.commons.lang3.StringEscapeUtils
import play.api.mvc._
import play.api.libs.iteratee._
import play.twirl.api._

import play.core.Execution.Implicits.internalContext

/**
 * Helper function to produce a Comet Enumeratee.
 *
 * Example:
 * {{{
 * val cometStream = Enumerator("A", "B", "C") &> Comet(callback = "console.log")
 * }}}
 *
 */
object Comet {

  /**
   * Typeclass for Comet message. Transform each value to a JavaScript message.
   */
  case class CometMessage[A](toJavascriptMessage: A => String)

  /**
   * Default typeclasses for CometMessage.
   */
  object CometMessage {

    /**
     * String messages.
     */
    implicit val stringMessages = CometMessage[String](str => "'" + StringEscapeUtils.escapeEcmaScript(str) + "'")

    /**
     * Json messages.
     */
    implicit val jsonMessages = CometMessage[play.api.libs.json.JsValue](json => json.toString())

  }

  /**
   * Create a Comet Enumeratee.
   *
   * @tparam E Type of messages handled by this comet stream.
   * @param callback Javascript function to call on the browser for each message.
   * @param initialChunk Initial chunk of data to send for browser compatibility (default to send 5Kb of blank data)
   */
  def apply[E](callback: String, initialChunk: Html = Html(Array.fill[Char](5 * 1024)(' ').mkString + "<html><body>"))(implicit encoder: CometMessage[E]) = new Enumeratee[E, Html] {

    def applyOn[A](inner: Iteratee[Html, A]): Iteratee[E, Iteratee[Html, A]] = {

      val fedWithInitialChunk = Iteratee.flatten(Enumerator(initialChunk) |>> inner)
      val eToScript = Enumeratee.map[E](data => Html("""<script type="text/javascript">""" + callback + """(""" + encoder.toJavascriptMessage(data) + """);</script>"""))
      eToScript.applyOn(fedWithInitialChunk)
    }
  }
}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Comet.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.