play.api.libs

package play.api.libs

Contains various APIs that are useful while developing web applications.

Attributes

Members list

Type members

Classlikes

object Codecs

Utilities for Codecs operations.

Utilities for Codecs operations.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Codecs.type
object Collections

Utilities functions for Collections

Utilities functions for Collections

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
object Comet

Helper function to produce a Comet using Pekko Streams.

Helper function to produce a Comet using Pekko Streams.

Please see https://en.wikipedia.org/wiki/Comet_(programming) for details of Comet.

Example:

 def streamClock() = Action {
   val df: DateTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss")
   val tickSource = Source.tick(0 millis, 100 millis, "TICK")
   val source = tickSource.map((tick) => df.format(ZonedDateTime.now()))
   Ok.chunked(source via Comet.flow("parent.clockChanged"))
 }

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Comet.type
object EventSource

This class provides an easy way to use Server Sent Events (SSE) as a chunked encoding, using an Pekko Source.

This class provides an easy way to use Server Sent Events (SSE) as a chunked encoding, using an Pekko Source.

Please see the Server-Sent Events specification for details.

An example of how to display an event stream:

 import java.time.ZonedDateTime
 import java.time.format.DateTimeFormatter
 import jakarta.inject.Singleton
 import org.apache.pekko.stream.scaladsl.Source
 import play.api.http.ContentTypes
 import play.api.libs.EventSource
 import play.api.mvc._

 import scala.concurrent.duration._

 def liveClock() = Action {
   val df: DateTimeFormatter = DateTimeFormatter.ofPattern("HH mm ss")
   val tickSource = Source.tick(0 millis, 100 millis, "TICK")
   val source = tickSource.map { (tick) =>
     df.format(ZonedDateTime.now())
   }
   Ok.chunked(source via EventSource.flow).as(ContentTypes.EVENT_STREAM)
 }

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
object Files

FileSystem utilities.

FileSystem utilities.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
Files.type
object JNDI

JNDI Helpers.

JNDI Helpers.

Attributes

Supertypes
class Object
trait Matchable
class Any
Self type
JNDI.type
case class Jsonp(padding: String, json: JsValue)

JSONP helper.

JSONP helper.

Example of use, provided the following route definition:

 GET  /my-service       Application.myService(callback: String)

The following action definition:

 def myService(callback: String) = Action {
   val json = ...
   Ok(Jsonp(callback, json))
 }

And the following request:

 GET /my-service?callback=foo

The response will have content type “application/javascript” and will look like the following:

 foo({...});

Another example, showing how to serve either JSON or JSONP from the same action, according to the presence of a “callback” parameter in the query string:

 def myService = Action { implicit request =>
   val json = ...
   request.queryString.get("callback").flatMap(_.headOption) match {
     case Some(callback) => Ok(Jsonp(callback, json))
     case None => Ok(json)
   }
 }

Attributes

Companion
object
Supertypes
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
object Jsonp

Attributes

Companion
class
Supertypes
trait Product
trait Mirror
class Object
trait Matchable
class Any
Self type
Jsonp.type