Package play.libs

Class Comet


  • public abstract class Comet
    extends Object
    Provides an easy way to use a Comet formatted output with Pekko Streams.

    There are two methods that can be used to convert strings and JSON, Comet.string and Comet.json. These methods build on top of the base method, Comet.flow, which takes a Flow of org.apache.pekko.util.ByteString and organizes it into Comet format.

       public Result liveClock() {
            final DateTimeFormatter df = DateTimeFormatter.ofPattern("HH mm ss");
            final Source tickSource = Source.tick(Duration.Zero(), Duration.create(100, MILLISECONDS), "TICK");
            final Source eventSource = tickSource.map((tick) -> df.format(ZonedDateTime.now()));
    
            final Source<ByteString, NotUsed> flow = eventSource.via(Comet.string("parent.clockChanged"));
            return ok().chunked(flow).as(Http.MimeTypes.HTML);
       }
     
    • Constructor Detail

      • Comet

        public Comet()
    • Method Detail

      • string

        public static Flow<String,​ByteString,​NotUsed> string​(String callbackName)
        Produces a Flow of escaped ByteString from a series of String elements. Calls out to Comet.flow internally.
        Parameters:
        callbackName - the javascript callback method.
        Returns:
        a flow of ByteString elements.
      • string

        public static Flow<String,​ByteString,​NotUsed> string​(String callbackName,
                                                                         String nonce)
        Produces a Flow of escaped ByteString from a series of String elements. Calls out to Comet.flow internally.
        Parameters:
        callbackName - the javascript callback method.
        nonce - The CSP nonce to use for the script tag. If null no nonce will be sent.
        Returns:
        a flow of ByteString elements.
      • json

        public static Flow<com.fasterxml.jackson.databind.JsonNode,​ByteString,​NotUsed> json​(String callbackName)
        Produces a flow of ByteString using `Json.stringify` from a Flow of JsonNode. Calls out to Comet.flow internally.
        Parameters:
        callbackName - the javascript callback method.
        Returns:
        a flow of ByteString elements.
      • json

        public static Flow<com.fasterxml.jackson.databind.JsonNode,​ByteString,​NotUsed> json​(String callbackName,
                                                                                                        String nonce)
        Produces a flow of ByteString using `Json.stringify` from a Flow of JsonNode. Calls out to Comet.flow internally.
        Parameters:
        callbackName - the javascript callback method.
        nonce - The CSP nonce to use for the script tag. If null no nonce will be sent.
        Returns:
        a flow of ByteString elements.
      • flow

        public static Flow<ByteString,​ByteString,​NotUsed> flow​(String callbackName)
        Produces a flow of ByteString with a prepended block and a script wrapper.
        Parameters:
        callbackName - the javascript callback method.
        Returns:
        a flow of ByteString elements.
      • flow

        public static Flow<ByteString,​ByteString,​NotUsed> flow​(String callbackName,
                                                                           String nonce)
        Produces a flow of ByteString with a prepended block and a script wrapper.
        Parameters:
        callbackName - the javascript callback method.
        nonce - The CSP nonce to use for the script tag. If null no nonce will be sent.
        Returns:
        a flow of ByteString elements.